/*
* 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
*/
/*
* Utility functions
*/
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sgs.h>
#include <libintl.h>
#include <debug.h>
#include "msg.h"
#include "_libld.h"
/*
* libld_malloc() and dz_map() are used for both performance and for ease of
* programming:
*
* Performance:
* The link-edit is a short lived process which doesn't really free much
* of the dynamic memory that it requests. Because of this, it is more
* important to optimize for quick memory allocations than the
* re-usability of the memory.
*
* waste the overhead of zeroing out these pages for calloc() requests.
*
* Memory Management:
* By doing all libld memory management through the ld_malloc routine
* it's much easier to free up all memory at the end by simply unmaping
* all of the blocks that were mapped in through dz_map(). This is much
* simpler then trying to track all of the libld structures that were
* dynamically allocate and are actually pointers into the ELF files.
*
* It's important that we can free up all of our dynamic memory because
* libld is used by ld.so.1 when it performs dlopen()'s of relocatable
* objects.
*
* Format:
* The memory blocks for each allocation store the size of the allocation
* in the first 8 bytes of the block. The pointer that is returned by
* libld_malloc() is actually the address of (block + 8):
*
* (addr - 8) block_size
* (addr) <allocated block>
*
* The size is retained in order to implement realloc(), and to perform
* the required memcpy(). 8 bytes are uses, as the memory area returned
* by libld_malloc() must be 8 byte-aligned. Even in a 32-bit environment,
* u_longlog_t pointers are employed.
*
* Map anonymous memory via MAP_ANON (added in Solaris 8).
*/
static void *
{
void *addr;
return (MAP_FAILED);
}
return (addr);
}
void *
{
void *vptr;
/*
* If this is the first allocation, or the allocation request is greater
* than the current free space available, allocate a new heap.
*/
/*
* Allocate a block that is at minimum 'HEAPBLOCK' size
*/
return (NULL);
}
/*
* Assign size to head of allocated block (used by realloc), and
* memory arena as then next 8-byte aligned offset.
*/
/*
* Increment free to point to next available block
*/
return (vptr);
}
void *
{
void *vptr;
return (libld_malloc(size));
/*
* Size of the allocated blocks is stored *just* before the blocks
* address.
*/
/*
* If the block actually fits then just return.
*/
return (ptr);
return (vptr);
}
void
/* ARGSUSED 0 */
{
}
/*
* Determine if a shared object definition structure already exists and if
* not create one. These definitions provide for recording information
* regarding shared objects that are still to be processed. Once processed
* shared objects are maintained on the ofl_sos list. The information
* recorded in this structure includes:
*
* o DT_USED requirements. In these cases definitions are added during
* mapfile processing of `-' entries (see map_dash()).
*
* o implicit NEEDED entries. As shared objects are processed from the
* command line so any of their dependencies are recorded in these
* structures for later processing (see process_dynamic()).
*
* o version requirements. Any explicit shared objects that have version
* dependencies on other objects have their version requirements recorded.
* In these cases definitions are added during mapfile processing of `-'
* entries (see map_dash()). Also, shared objects may have versioning
* requirements on their NEEDED entries. These cases are added during
* their version processing (see vers_need_process()).
*
* Note: Both process_dynamic() and vers_need_process() may generate the
* initial version definition structure because you can't rely on what
* section (.dynamic or .SUNW_version) may be processed first from any
* input file.
*/
Sdf_desc *
{
return (sdf);
return (NULL);
}
Sdf_desc *
{
return (sdf);
}
/*
* Add a string, separated by a colon, to an existing string. Typically used
* to maintain filter, rpath and audit names, of which there is normally only
* one string supplied anyway.
*/
char *
{
char *new;
if (old) {
char *_str;
/*
* If an original string exists, make sure this new string
* doesn't get duplicated.
*/
((*_str == '\0') ||
return (old);
}
return ((char *)S_ERROR);
} else {
return ((char *)S_ERROR);
}
return (new);
}
/*
* The GNU ld '-wrap=XXX' and '--wrap=XXX' options correspond to our
* '-z wrap=XXX'. When str2chr() does this conversion, we end up with
* the return character set to 'z' and optarg set to 'XXX'. This callback
* changes optarg to include the missing wrap= prefix.
*
* exit:
* Returns c on success, or '?' on error.
*/
static int
str2chr_wrap_cb(int c)
{
char *str;
return ('?');
return (c);
}
/*
* Determine whether this string, possibly with an associated option, should
* be translated to an option character. If so, update the optind and optarg
* and optopt as described for short options in getopt(3c).
*
* entry:
* lml - Link map list for debug messages
* ndx - Starting optind for current item
* argc, argv - Command line arguments
* arg - Option to be examined
* c, opt - Option character (c) and corresponding long name (opt)
* optsz - 0 if option does not accept a value. If option does
* accept a value, strlen(opt), giving the offset to the
* value if the option and value are combined in one string.
* cbfunc - NULL, or pointer to function to call if a translation is
* successful.
*/
static int
{
if (optsz == 0) {
/*
* Compare a single option (ie. there's no associated option
* argument).
*/
optind += 1;
optopt = c;
return (c);
}
/*
* Otherwise, compare the option name, which may be
* concatenated with the option argument.
*/
/*
* Optarg is the next argument (white space separated).
* Make sure an optarg is available, and if not return
* a failure to prevent any fall-through to the generic
* getopt() processing.
*
* Since we'll be completely failing this option we
* don't want to update optopt with the translation,
* but also need to set it to _something_. Setting it
* to the '-' of the argument causes us to behave
* correctly.
*/
return ('?');
}
optind++;
} else {
/*
* with a "=" separator. If this is the case, remove
* the separator.
*/
optind++;
if (*optarg == '=') {
if (*(++optarg) == '\0') {
return ('?');
}
}
}
c = (*cbfunc)(c);
optopt = c;
return (c);
}
return (0);
}
/*
* Parse an individual option. The intent of this function is to determine if
* any known, non-Solaris options have been passed to ld(1). This condition
* can occur as a result of build configuration tools, because of users
* familiarity with other systems, or simply the users preferences. If a known
* non-Solaris option can be determined, translate that option into the Solaris
* counterpart.
*
* This function will probably never be a complete solution, as new, non-Solaris
* options are discovered, their translation will have to be added. Other
* non-Solaris options are incompatible with the Solaris link-editor, and will
* never be recognized. We support what we can.
*/
int
{
int c;
switch (*arg) {
case 'r':
/* Translate -rpath <optarg> to -R <optarg> */
MSG_ARG_T_RPATH_SIZE, NULL)) != 0) {
return (c);
}
break;
case 's':
/* Translate -shared to -G */
return (c);
/* Translate -soname <optarg> to -h <optarg> */
MSG_ARG_T_SONAME_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'w':
/* Translate -wrap to -z wrap= */
return (c);
}
break;
case '(':
/*
* Translate -( to -z rescan-start
*/
0) {
return (c);
}
break;
case ')':
/*
* Translate -) to -z rescan-end
*/
0) {
return (c);
}
break;
case '-':
switch (*(arg + 1)) {
case 'a':
/*
* Translate --allow-multiple-definition to
* -zmuldefs
*/
0) {
optarg =
(char *)MSG_ORIG(MSG_ARG_MULDEFS);
return (c);
/*
* Translate --auxiliary <optarg> to
* -f <optarg>
*/
MSG_ARG_T_AUXFLTR_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'd':
/*
* Translate --dynamic-linker <optarg> to
* -I <optarg>
*/
MSG_ARG_T_INTERP_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'e':
/* Translate --entry <optarg> to -e <optarg> */
MSG_ARG_T_ENTRY_SIZE, NULL)) != 0) {
return (c);
}
/*
* Translate --end-group to -z rescan-end
*/
0, NULL)) != 0) {
optarg = (char *)
return (c);
}
break;
case 'f':
/*
* Translate --fatal-warnings to
* -z fatal-warnings.
*/
0, NULL)) != 0) {
optarg = (char *)
return (c);
}
/* Translate --filter <optarg> to -F <optarg> */
MSG_ARG_T_STDFLTR_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'h':
/* Translate --help to -zhelp */
0) {
return (c);
}
break;
case 'l':
/*
* Translate --library <optarg> to -l <optarg>
*/
MSG_ARG_T_LIBRARY_SIZE, NULL)) != 0) {
return (c);
/*
* Translate --library-path <optarg> to
* -L <optarg>
*/
MSG_ARG_T_LIBPATH_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'n':
/*
* Translate --no-fatal-warnings to
* -z nofatal-warnings.
*/
0, NULL)) != 0) {
optarg = (char *)
return (c);
}
/* Translate --no-undefined to -zdefs */
0) {
return (c);
/*
* Translate --no-whole-archive to
* -z defaultextract
*/
0, NULL)) != 0) {
optarg =
(char *)MSG_ORIG(MSG_ARG_DFLEXTRT);
return (c);
}
break;
case 'o':
/* Translate --output <optarg> to -o <optarg> */
MSG_ARG_T_OUTPUT_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'r':
/* Translate --relocatable to -r */
NULL)) != 0) {
return (c);
}
break;
case 's':
/* Translate --strip-all to -s */
0) {
return (c);
}
/*
* Translate --start-group to -z rescan-start
*/
0, NULL)) != 0) {
optarg = (char *)
return (c);
}
break;
case 'u':
/*
* Translate --undefined <optarg> to
* -u <optarg>
*/
MSG_ARG_T_UNDEF_SIZE, NULL)) != 0) {
return (c);
}
break;
case 'v':
/* Translate --version to -V */
0) {
return (c);
}
break;
case 'w':
/*
* Translate --whole-archive to -z alltextract
*/
0, NULL)) != 0) {
optarg =
(char *)MSG_ORIG(MSG_ARG_ALLEXTRT);
return (c);
}
/*
* Translate --wrap to -z wrap=
*/
0) {
return (c);
}
break;
}
break;
}
}
/*
* It is possible that a "-Wl," argument has been used to
* specify an option. This isn't advertized ld(1) syntax, but
* compiler drivers and configuration tools, have been known to
* pass this compiler option to ld(1). Strip off the "-Wl,"
* prefix and pass the option through.
*/
c = optarg[MSG_ARG_T_WL_SIZE];
}
}
return (c);
}
/*
* A compare routine for Isd_node AVL trees.
*/
int
{
int rc;
return (1);
return (-1);
if (rc > 0)
return (1);
if (rc < 0)
return (-1);
return (0);
}
/*
* Messaging support - funnel everything through dgettext().
*/
const char *
{
}
/*
* Determine whether a symbol name should be demangled.
*/
const char *
{
if (demangle_flag)
return (Elf_demangle_name(name));
else
return (name);
}
/*
* Compare a series of platform or machine hardware names.
*/
int
{
int match = 0;
return (1);
continue;
match++;
break;
}
}
return (0);
return (1);
}