README.XLINK revision ba2be53024c0b999e74ba9adcd7d80fec5df8c57
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# CDDL HEADER START
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# The contents of this file are subject to the terms of the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# Common Development and Distribution License (the "License").
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# You may not use this file except in compliance with the License.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# or http://www.opensolaris.org/os/licensing.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# See the License for the specific language governing permissions
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# and limitations under the License.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# When distributing Covered Code, include this CDDL HEADER in each
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# If applicable, add the following below this CDDL HEADER, with the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# fields enclosed by brackets "[]" replaced with your own identifying
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# information: Portions Copyright [yyyy] [name of copyright owner]
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# CDDL HEADER END
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# Use is subject to license terms.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston#
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston# ident "%Z%%M% %I% %E% SMI"
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonNotes On Cross Link-Editor Support in libld.so
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston-----------------------------------------
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThe Solaris link-editor is used in two contexts:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston 1) The standard ld command
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi 2) Via the runtime linker (ld.so.1), when a program
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi calls dlopen() on a relocatable object (ET_REL).
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiTo support both uses, it is packaged as a sharable library (libld.so).
23a276b1252962c987a613be470dde26561247b8huestonThe ld command is therefore a simple wrapper that uses libld.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonlibld.so is a cross linker. This means that it can link objects for
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestona system other than the system running the link-editor (e.g. A link-editor
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonrunning on an amd64 system processing sparc objects). This means that every
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiinstance of libld.so contains code for building objects for every supported
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestontarget. This unlike GNU ld, where you build gld specifically for the
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiplatform you're targeting. This is possible because unlike gld, we only
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonsupport Solaris, with a small number of platforms.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonAt initialization, the caller of libld.so specifies the type of objects
0eb822a1c0c2bea495647510b75f77f0e57633ebcindibeing linked. By default, the ld command determines the machine type and
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiclass of the object being generated from the first ELF object processed
0eb822a1c0c2bea495647510b75f77f0e57633ebcindifrom the command line. The -64 and -ztarget options exists to change this
0eb822a1c0c2bea495647510b75f77f0e57633ebcindidefault, which is useful when creating an object entirely from an archive
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonlibrary or a mapfile. During initialization, the link-editor configures
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiitself to build an output object of the specified type. This is done via
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonindirection, using the global ld_targ structure to access code, data, and
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonconstants for the specified target.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThere are two types of source files used to build libld.so:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston 1) Common code used for all targets
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston 2) Target specific code used only when linking for
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston a given target.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonAll of these files reside in usr/src/cmd/sgs/libld/common. However,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonit is easy to see which files belong in each category by examining
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe object lists maintained in usr/src/cmd/sgs/libld/Makefile.com.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIn addition, the target-specific files usually include the target
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonin their name (i.e. machrel.sparc.c).
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiAlthough the target dependent and independent (common) code is well separated,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthey are interdependent. For example, the common code is aware of
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe target-specific section types that can occur only for some targets
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston(i.e. SHT_AMD64_UNWIND). This is not an architecture that allows
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonfor arbitrary target support to be dynamically plugged into an unchanged
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonplatform independent core. Rather, it is an organization that allows
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestona common core to support all the targets it knows about in a way that
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiis understandable and maintainable. A truly pluggable architecture
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiwould be considerably more opaque and complex, and is neither necessary,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindinor desirable, given the wide commonality between modern computer
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonarchitectures.
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIt is possible to add support for new targets to libld.so. The process
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonof doing so is largely a matter of examining the files for existing
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiplatforms, studying the ABI for the new target platform, and then
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonfilling in the missing pieces for the new target. The remainder of this
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonfile consists of sections that describe some of the issues and steps
0eb822a1c0c2bea495647510b75f77f0e57633ebcindithat you will encounter in adding a new target.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston-----------------------------------------------------------------------------
23a276b1252962c987a613be470dde26561247b8huestonThe relocation code used by ld is shared by the runtime linker (ld.so.1)
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonand by the kernel module loader (ktrld), and is therefore found under
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonusr/src/uts. You must add code for a relocation engine to support the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonnew target. To do so, examine the common header files:
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/uts/common/krtld/reloc.h
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi usr/src/uts/common/krtld/reloc_defs.h
23a276b1252962c987a613be470dde26561247b8hueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston and the existing relocation engines:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/uts/intel/amd64/krtld/doreloc.c
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/uts/intel/ia32/krtld/doreloc.c
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/uts/sparc/krtld/doreloc.c
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThe ABI for the target platform will be the primary information
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiyou require. If your new system has attributes not found in an existing
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestontarget, you may have to add/modify fields in the Rel_entry struct typedef
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston(reloc_defs.h), or you may have to add new flags. Either sort of change
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonmay require you to also modify the existing relocation engines, and
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonundoubtedly the common code in libld.so as well.
23a276b1252962c987a613be470dde26561247b8hueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonWhen compiled for use by libld, the relocation engine requires an
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiargument named "bswap". Each relocation engine must be prepared to
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonswap the data bytes it is operating on. This support allows a link-editor
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonrunning on a platform with a different byte order than the target to
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonbuild objects for that target. To see how this is implemented, and how
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonto ifdef that support so it only exists in the libld version of
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe engine, examine the code for the existing engines.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston-----------------------------------------------------------------------------
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonYou must create a target subdirectory in usr/src/cmd/sgs/include,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiand construct a machdep_XXX.h file (where XXX is the name of the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestontarget). The machdep files for the current platforms can be helpful:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/cmd/sgs/include/sparc/machdep_sparc.h
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/cmd/sgs/include/i386/machdep_x86.h
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonNote that these files support both the 32 and 64-bit versions of
0eb822a1c0c2bea495647510b75f77f0e57633ebcindia given platform, so there is only one subdirectory and machdep
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonfile for each platform (i.e. "sparc", instead of "sparc" and "sparcv9").
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonOnce you have created the target machdep_XXX.h file, you must edit:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston usr/src/cmd/sgs/include/machdep.h
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonand add a #include for your new file to it, surrounded by the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonappropriate #ifdef for the target platform.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThis two level structure allows us to #include machdep information
23a276b1252962c987a613be470dde26561247b8huestonin two different ways:
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston 1) Code that wants support for the current platform,
23a276b1252962c987a613be470dde26561247b8hueston regardless of which platform that is, can include
23a276b1252962c987a613be470dde26561247b8hueston usr/src/cmd/sgs/include/machdep.h. The runtime linker
23a276b1252962c987a613be470dde26561247b8hueston (ld.so.1) is the primary consumer of this form.
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi 2) Code that needs to support multiple targets must never
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi include the generic machdep.h from (1) above. Instead,
23a276b1252962c987a613be470dde26561247b8hueston such code explicitly includes the machdep file for the target
23a276b1252962c987a613be470dde26561247b8hueston it is interested in. For example:
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston #include <sparc/machdep_sparc.h>
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston libld.so uses this form to build non-native target
23a276b1252962c987a613be470dde26561247b8hueston code.
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8huestonYou will find that most of the constants defined in the target
23a276b1252962c987a613be470dde26561247b8huestonmachdep file end up as initialization for the information that
23a276b1252962c987a613be470dde26561247b8huestonlibld.so accesses via the ld_targ global variable.
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston-----------------------------------------------------------------------------
23a276b1252962c987a613be470dde26561247b8huestonStudy the definition of the Target typedef in
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston usr/src/cmd/sgs/libld/common/_libld.h
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8huestonThis is the type of the ld_targ global variable. Filling in a complete
23a276b1252962c987a613be470dde26561247b8huestoncopy of this definition is the primary task involved in adding support
23a276b1252962c987a613be470dde26561247b8huestonfor a new target to libld.so, so it will be helpful to be familiar with
23a276b1252962c987a613be470dde26561247b8huestonit before you dive deeper. libld follows two simple rules with regards
23a276b1252962c987a613be470dde26561247b8huestonto ld_targ, and the Target type:
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston 1) The target-independent common code can only access
23a276b1252962c987a613be470dde26561247b8hueston target-dependent code or data via the ld_targ global
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi variable.
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston 2) The target-dependent code can access the common
23a276b1252962c987a613be470dde26561247b8hueston code or data freely.
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8huestonA given link-editor invocation is always for a single target. The choice
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiof target is made at initialization, and does not change within a
23a276b1252962c987a613be470dde26561247b8huestonsingle link. Code for the other targets is present, but is not
23a276b1252962c987a613be470dde26561247b8huestonaccessed.
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston-----------------------------------------------------------------------------
23a276b1252962c987a613be470dde26561247b8huestonFiles containing the target-specific code to support the new
23a276b1252962c987a613be470dde26561247b8huestonplatform must be added to libld.so. Examine the object lists
23a276b1252962c987a613be470dde26561247b8huestonin usr/src/cmd/sgs/libld/Makefile.com to see the files for existing
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiplatforms, and read those files to get a sense of what is required.
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
23a276b1252962c987a613be470dde26561247b8huestonAmong the other files, every platform will have a file named
23a276b1252962c987a613be470dde26561247b8huestonmachrel.XXX.c. This file contains the relocation-related functions,
23a276b1252962c987a613be470dde26561247b8huestonand it also contains an init function for the target. This init function
23a276b1252962c987a613be470dde26561247b8huestonis responsible for initializing the ld_targ global variable so that
23a276b1252962c987a613be470dde26561247b8huestonthe common code will use the code and definitions for your
23a276b1252962c987a613be470dde26561247b8huestontarget.
23a276b1252962c987a613be470dde26561247b8hueston
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiYou should start by creating a machrel.XXX.c file for your new
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditarget. Add other files as needed. Be aware that any functions or
23a276b1252962c987a613be470dde26561247b8huestonvariables you place in these target-dependent files must either
23a276b1252962c987a613be470dde26561247b8huestonbe static, or must have names that will not collide with the names
23a276b1252962c987a613be470dde26561247b8huestonused by the rest of libld.so. The easiest way to do this is to
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiadd a target suffix to the end of all such non-local names
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi(i.e. foo_sparc() instead of foo()).
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8huestonThe existing code is the documentation for this phase of things: The
23a276b1252962c987a613be470dde26561247b8huestonbest way to determine what a given function should do is to read the
0eb822a1c0c2bea495647510b75f77f0e57633ebcindicode for other platforms, taking into account the similarities and
0eb822a1c0c2bea495647510b75f77f0e57633ebcindidifferences in the ABI for your new target and those existing ones.
23a276b1252962c987a613be470dde26561247b8hueston
23a276b1252962c987a613be470dde26561247b8hueston-----------------------------------------------------------------------------
23a276b1252962c987a613be470dde26561247b8huestonYou may find that your new target requires support for new concepts
23a276b1252962c987a613be470dde26561247b8huestonnot found in other targets. A common example of this might be
0eb822a1c0c2bea495647510b75f77f0e57633ebcindia new target specific ELF section type (i.e. SHT_AMD64_UNWIND). Another
23a276b1252962c987a613be470dde26561247b8huestonmight be details involving PIC code and PLT generation (as found for
23a276b1252962c987a613be470dde26561247b8huestonsparc). It may be necessary to add new fields to the ld_targ global
23a276b1252962c987a613be470dde26561247b8huestonvariable, and to modify the libld.so common code to use these new
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonfields.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIt is a standard convention that NULL function pointers are used to
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonrepresent functionality not required by a given target. Although the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestoncommon code can consult ld_targ.t_m.m_mach to determine the target it
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonis linking for, and although there is some code that does this, it
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonis generally undesirable and unnecessary. Instead, the common code
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonshould test for such pointers, as with this sparc-specific example
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonfrom update.c:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston /*
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston * Assign a got offset if necessary.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston */
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston if ((ld_targ.t_mr.mr_assign_got != NULL) &&
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston return ((Addr)S_ERROR);
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIt may be tempting to include information in the comment that explains
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe target specific nature of this, and that may even be appropriate.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonConsider however, that a new target may come along with the same feature
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonlater, and if that occurs, your comments will instantly be dated. In general,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe use of ld_targ is a strong hint to the reader that they should go read
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe target-specific code referenced to understand what is going on. It is
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonbest to supply comments at the call site that describe the operation
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiin generic terms (i.e. "assign a got if necessary") instead of in
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonexplicit target terms (i.e. "Assign a sparc got if necessary"). Of
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestoncourse, some features are extremely target-specific (like amd64 unwind
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonsections), and it doesn't really help to be obscure in such cases.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThis is a judgement call.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIf you do add a new field to ld_targ that uses NULL to represent
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonan option feature *YOU MUST DOCUMENT IT AS SUCH*. You will find
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestoncomments in _libld.h for existing optional fields. It suffices to
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonadd a comment for your new field. In the absence of such a comment,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe common code assumes that all function pointers are safe to call
0eb822a1c0c2bea495647510b75f77f0e57633ebcindithrough (dereference) without first testing them.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston-----------------------------------------------------------------------------
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonByte swapping is a big issue in cross linking, as the system running
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe link-editor may have the opposite byte order from the target. It is
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonimportant to know when, and when not, to swap bytes.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIf the build system and target have different byte orders, the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonFLG_OF1_ENCDIFF bit of the ofl_flags field of the output file
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestondescriptor will be set. If this bit is not set, the target and
0eb822a1c0c2bea495647510b75f77f0e57633ebcindisystem byte orders are the same, and no byte swapping
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonis required.
23a276b1252962c987a613be470dde26561247b8hueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonlibld uses libelf to read and write objects. libelf automatically
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonswaps bytes for the sections it knows about, such as symbol tables,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonrelocation records, and the usual ELF plumbing. It is therefore never
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonnecessary for your code to swap the bytes in this data. If you find that
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthis is not the case, you have probably uncovered a bug in libelf that
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonyou should look into.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThe big exception to libelf transparently handling byte swapping is
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonprogbits sections (SHT_PROGBITS). libelf does not understand program
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestoncode or data as anything other than a series of byte values, and as such,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestoncannot do byte swapping for them. If your code examines or modifies
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonsuch data, you are responsible for handling the byte swapping required.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThe OFL_SWAP_RELOC macros found in _libld.h can be helpful in making such
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestondeterminations. You should use these macros instead of writing your own
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestontests for this, as they have high documentation value. If you find they
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestondon't handle your target, add a new one that does.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonGOT and PLT sections are SHT_PROGBITS. You will probably find
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthat the vast majority of byte swapping you have to handle
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonconcern the handling of these items.
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonlibld contains generic functions for byte swapping:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston ld_bswap_Word();
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston ld_bswap_Xword();
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThese functions are built on top of the of the BSWAP_ macros found
23a276b1252962c987a613be470dde26561247b8huestonin usr/src/cmd/sgs/include/_machelf.h:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
724365f7556fc4201fdb11766ebc6bd918523130sethg BSWAP_HALF
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston BSWAP_WORD
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston BSWAP_XWORD
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonWhen copying data from one address to another in a cross link environment,
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe source and/or destination addresses may not have adequate alignment for
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonthe data type being copied. For example, a sparc platform cannot access
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi8-byte data types on 4-byte boundaries, but it might need to do so when
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonlinking X86 objects where the alignment of such data can be 4. The
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonUL_ASSIGN macros can be used to copy potentially unaligned data:
23a276b1252962c987a613be470dde26561247b8hueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston UL_ASSIGN_HALF
724365f7556fc4201fdb11766ebc6bd918523130sethg UL_ASSIGN_WORD
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston UL_ASSIGN_XWORD
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonThe UL_ASSIGN_BSWAP macros do unaligned copies, and also perform
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonbyte swapping when the linker host and target byte orders are
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestondifferent:
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston UL_ASSIGN_BSWAP_HALF
23a276b1252962c987a613be470dde26561247b8hueston UL_ASSIGN_BSWAP_WORD
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston UL_ASSIGN_BSWAP_XWORD
724365f7556fc4201fdb11766ebc6bd918523130sethg
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonIf you are reading/writing to relocation data, the following
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonroutines understand relocation records and will get/set the
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chuestonproper amount of data while handling any needed swapping:
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
724365f7556fc4201fdb11766ebc6bd918523130sethg ld_reloc_targval_get()
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston ld_reloc_targval_set()
0b6016e6ff70af39f99c9cc28e0c2207c8f5413chueston
Byte swapping is a fertile area for mistakes. If you're having trouble
getting a successful link in a cross link situation, you should always
do the experiment of doing the link on a platform with the same byte
order as the target. If that link succeeds, then you are looking at
a bug involving incorrect byte swapping.
-----------------------------------------------------------------------------
As mentioned above, incorrect byte swapping is a common
error when developing libld target specific code. In addition to
trying a build machine with the same byte order as the target, elfdump
can also be a powerful tool for debugging. The first step with
elfdump is to simply dump everything and read it looking for obviously
bad information:
% elfdump outobj 2>&1 | more
elfdump tries to do sanity checking on the objects it
displays. Hence, the following command is a a common
idiom:
% elfdump outobj > /dev/null
Any problems with the file that elfdump can detect will be
written to stderr.
-----------------------------------------------------------------------------
Once you have the target-specific code in place, you must modify the
libld initialization code so that it will know how to use it. This
logic is found in
usr/src/cmd/sgs/libld/common/ldmain.c
in the function ld_init_target().
-----------------------------------------------------------------------------
The ld front end program that uses libld must be modified so that
the "-z target=platform" command line option recognizes your
new target. This code is found in
usr/src/cmd/sgs/ld/common
The change consists of adding an additional strcasecmp() to the
command line processing for -ztarget.
-----------------------------------------------------------------------------
You probably changed things getting your target integrated.
Please update this document to reflect your changes.