17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55aefb655Srie * Common Development and Distribution License (the "License"). 65aefb655Srie * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21552ff457Srie 227c478bd9Sstevel@tonic-gate /* 23*bf994817SAli Bahrami * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _RELOC_DOT_H 277c478bd9Sstevel@tonic-gate #define _RELOC_DOT_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #if defined(_KERNEL) 307c478bd9Sstevel@tonic-gate #include <sys/bootconf.h> 317c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 327c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h> 337c478bd9Sstevel@tonic-gate #else 345aefb655Srie #include <rtld.h> 35552ff457Srie #include <conv.h> 367c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 377c478bd9Sstevel@tonic-gate 38ba2be530Sab196087 #include "reloc_defs.h" 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #ifdef __cplusplus 417c478bd9Sstevel@tonic-gate extern "C" { 427c478bd9Sstevel@tonic-gate #endif 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * Global include file for relocation common code. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 495aefb655Srie * In user land, redefine the relocation table and relocation engine to be 50ba2be530Sab196087 * class/machine specific if necessary. This allows multiple engines to 51ba2be530Sab196087 * reside within a single instance of libld. 525aefb655Srie */ 535aefb655Srie #if !defined(_KERNEL) 54ba2be530Sab196087 55ba2be530Sab196087 #if defined(DO_RELOC_LIBLD) 56ba2be530Sab196087 #undef DO_RELOC_LIBLD 57ba2be530Sab196087 #endif 58ba2be530Sab196087 59ba2be530Sab196087 #if defined(DO_RELOC_LIBLD_X86) 60ba2be530Sab196087 61ba2be530Sab196087 #define DO_RELOC_LIBLD 625aefb655Srie #if defined(_ELF64) 63ba2be530Sab196087 #define do_reloc_ld do64_reloc_ld_x86 64ba2be530Sab196087 #define reloc_table reloc64_table_x86 65ba2be530Sab196087 #else 66ba2be530Sab196087 #define do_reloc_ld do32_reloc_ld_x86 67ba2be530Sab196087 #define reloc_table reloc32_table_x86 68ba2be530Sab196087 #endif 69ba2be530Sab196087 70ba2be530Sab196087 #elif defined(DO_RELOC_LIBLD_SPARC) 71ba2be530Sab196087 72ba2be530Sab196087 #define DO_RELOC_LIBLD 73ba2be530Sab196087 #if defined(_ELF64) 74ba2be530Sab196087 #define do_reloc_ld do64_reloc_ld_sparc 75ba2be530Sab196087 #define reloc_table reloc64_table_sparc 76ba2be530Sab196087 #else 77ba2be530Sab196087 #define do_reloc_ld do32_reloc_ld_sparc 78ba2be530Sab196087 #define reloc_table reloc32_table_sparc 79ba2be530Sab196087 #endif 80ba2be530Sab196087 81ba2be530Sab196087 #else /* rtld */ 82ba2be530Sab196087 83ba2be530Sab196087 #if defined(_ELF64) 84f3324781Sab196087 #define do_reloc_rtld do64_reloc_rtld 855aefb655Srie #define reloc_table reloc64_table 865aefb655Srie #else 87f3324781Sab196087 #define do_reloc_rtld do32_reloc_rtld 885aefb655Srie #define reloc_table reloc32_table 895aefb655Srie #endif 90ba2be530Sab196087 915aefb655Srie #endif 925aefb655Srie 93ba2be530Sab196087 #endif /* !_KERNEL */ 94ba2be530Sab196087 955aefb655Srie /* 965aefb655Srie * Relocation table and macros for testing relocation table flags. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate extern const Rel_entry reloc_table[]; 997c478bd9Sstevel@tonic-gate 100ba2be530Sab196087 #define IS_PLT(X) RELTAB_IS_PLT(X, reloc_table) 101ba2be530Sab196087 #define IS_GOT_RELATIVE(X) RELTAB_IS_GOT_RELATIVE(X, reloc_table) 102ba2be530Sab196087 #define IS_GOT_PC(X) RELTAB_IS_GOT_PC(X, reloc_table) 103ba2be530Sab196087 #define IS_GOTPCREL(X) RELTAB_IS_GOTPCREL(X, reloc_table) 104ba2be530Sab196087 #define IS_GOT_BASED(X) RELTAB_IS_GOT_BASED(X, reloc_table) 105ba2be530Sab196087 #define IS_GOT_OPINS(X) RELTAB_IS_GOT_OPINS(X, reloc_table) 106ba2be530Sab196087 #define IS_GOT_REQUIRED(X) RELTAB_IS_GOT_REQUIRED(X, reloc_table) 107ba2be530Sab196087 #define IS_PC_RELATIVE(X) RELTAB_IS_PC_RELATIVE(X, reloc_table) 108ba2be530Sab196087 #define IS_ADD_RELATIVE(X) RELTAB_IS_ADD_RELATIVE(X, reloc_table) 109ba2be530Sab196087 #define IS_REGISTER(X) RELTAB_IS_REGISTER(X, reloc_table) 110ba2be530Sab196087 #define IS_NOTSUP(X) RELTAB_IS_NOTSUP(X, reloc_table) 111ba2be530Sab196087 #define IS_SEG_RELATIVE(X) RELTAB_IS_SEG_RELATIVE(X, reloc_table) 112ba2be530Sab196087 #define IS_EXTOFFSET(X) RELTAB_IS_EXTOFFSET(X, reloc_table) 113ba2be530Sab196087 #define IS_SEC_RELATIVE(X) RELTAB_IS_SEC_RELATIVE(X, reloc_table) 114ba2be530Sab196087 #define IS_TLS_INS(X) RELTAB_IS_TLS_INS(X, reloc_table) 115ba2be530Sab196087 #define IS_TLS_GD(X) RELTAB_IS_TLS_GD(X, reloc_table) 116ba2be530Sab196087 #define IS_TLS_LD(X) RELTAB_IS_TLS_LD(X, reloc_table) 117ba2be530Sab196087 #define IS_TLS_IE(X) RELTAB_IS_TLS_IE(X, reloc_table) 118ba2be530Sab196087 #define IS_TLS_LE(X) RELTAB_IS_TLS_LE(X, reloc_table) 119ba2be530Sab196087 #define IS_LOCALBND(X) RELTAB_IS_LOCALBND(X, reloc_table) 120ba2be530Sab196087 #define IS_SIZE(X) RELTAB_IS_SIZE(X, reloc_table) 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1235aefb655Srie * Relocation engine. 124f3324781Sab196087 * 125f3324781Sab196087 * The do_reloc() code is used in three different places: The kernel, 126*bf994817SAli Bahrami * the link-editor, and the runtime linker. All three convey the same 127*bf994817SAli Bahrami * basic information with the first 5 arguments: 128*bf994817SAli Bahrami * 129*bf994817SAli Bahrami * 1) Relocation type. The kernel and runtime linker pass this as 130*bf994817SAli Bahrami * an integer value, while the link-editor passes it as a Rel_desc 131*bf994817SAli Bahrami * descriptor. The relocation engine only looks at the rel_rtype 132*bf994817SAli Bahrami * field of this descriptor, and does not examine the other fields, 133*bf994817SAli Bahrami * which are explicitly allowed to contain garbage. 134*bf994817SAli Bahrami * 2) Address of offset 135*bf994817SAli Bahrami * 3) Address of value 136*bf994817SAli Bahrami * 4) Name of symbol associated with the relocation, used if it is 137*bf994817SAli Bahrami * necessary to report an error. The kernel and runtime linker pass 138*bf994817SAli Bahrami * directly as a string pointer. The link-editor passes the address 139*bf994817SAli Bahrami * of a rel_desc_sname_func_t function, which can be called by do_reloc(), 140*bf994817SAli Bahrami * passing it the Rel_desc pointer (argument 1, above), to obtain the 141*bf994817SAli Bahrami * string pointer. 142*bf994817SAli Bahrami * 5) String giving the source file for the relocation. 143*bf994817SAli Bahrami * 144*bf994817SAli Bahrami * In addition: 145f3324781Sab196087 * - The linker and rtld want a link map pointer argument 146f3324781Sab196087 * - The linker wants to pass a byte swap argument that tells 147f3324781Sab196087 * the relocation engine that the data it is relocating 148f3324781Sab196087 * has the opposite byte order of the system running the 149f3324781Sab196087 * linker. 150ba2be530Sab196087 * - The linker is a cross-linker, meaning that it can examine 151ba2be530Sab196087 * relocation records for target hosts other than that of 152ba2be530Sab196087 * the currently running system. This means that multiple 153ba2be530Sab196087 * versions of the relocation code must be able to reside 154ba2be530Sab196087 * in a single program, without namespace clashes. 155f3324781Sab196087 * 156f3324781Sab196087 * To ensure that there is never any confusion about which version is 157f3324781Sab196087 * being linked to, we give each variant a different name, even though 158f3324781Sab196087 * each one is generated from the same source code. 159f3324781Sab196087 * 160f3324781Sab196087 * do_reloc_krtld() 161f3324781Sab196087 * The kernel version is provided if the _KERNEL macro is defined. 162f3324781Sab196087 * 163f3324781Sab196087 * do_reloc_ld() 164ba2be530Sab196087 * The ld version is provided if the DO_RELOC_LIBLD_ macro is defined. 165f3324781Sab196087 * 166f3324781Sab196087 * do_reloc_rtld() 167f3324781Sab196087 * The rtld version is provided if neither _KERNEL or DO_RELOC_LIBLD 168f3324781Sab196087 * are defined. 169f3324781Sab196087 * 170f3324781Sab196087 * Implementations of do_reloc() should use these same macros to 171f3324781Sab196087 * conditionalize any code not used by all three versions. 1727c478bd9Sstevel@tonic-gate */ 173f3324781Sab196087 #if defined(_KERNEL) 174f3324781Sab196087 extern int do_reloc_krtld(uchar_t, uchar_t *, Xword *, const char *, 175f3324781Sab196087 const char *); 176f3324781Sab196087 #elif defined(DO_RELOC_LIBLD) 177*bf994817SAli Bahrami extern int do_reloc_ld(Rel_desc *, uchar_t *, Xword *, 178*bf994817SAli Bahrami rel_desc_sname_func_t, const char *, int, void *); 179f3324781Sab196087 #else 180f3324781Sab196087 extern int do_reloc_rtld(uchar_t, uchar_t *, Xword *, const char *, 1815aefb655Srie const char *, void *); 182f3324781Sab196087 #endif 183552ff457Srie 1847c478bd9Sstevel@tonic-gate #if defined(_KERNEL) 1857c478bd9Sstevel@tonic-gate /* 186552ff457Srie * These are macro's that are only needed for krtld. Many of these are already 187552ff457Srie * defined in the sgs/include files referenced by ld and rtld 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate #define S_MASK(n) ((1l << (n)) - 1l) 1907c478bd9Sstevel@tonic-gate #define S_INRANGE(v, n) (((-(1l << (n)) - 1l) < (v)) && ((v) < (1l << (n)))) 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 193552ff457Srie * Message strings used by doreloc(). 1947c478bd9Sstevel@tonic-gate */ 1957c478bd9Sstevel@tonic-gate #define MSG_STR_UNKNOWN "(unknown)" 196552ff457Srie 197552ff457Srie #define MSG_REL_PREGEN "relocation error: %s: " 198552ff457Srie #define MSG_REL_PREFIL "relocation error: file %s: " 199552ff457Srie #define MSG_REL_FILE "file %s: " 200552ff457Srie #define MSG_REL_SYM "symbol %s: " 201552ff457Srie #define MSG_REL_VALUE "value 0x%llx " 2027c478bd9Sstevel@tonic-gate #define MSG_REL_LOSEBITS "loses %d bits at " 2037c478bd9Sstevel@tonic-gate 204552ff457Srie #define MSG_REL_UNIMPL "unimplemented relocation type: %d" 205552ff457Srie #define MSG_REL_UNSUPSZ "offset size (%d bytes) is not supported" 206552ff457Srie #define MSG_REL_NONALIGN "offset 0x%llx is non-aligned" 207552ff457Srie #define MSG_REL_UNNOBITS "unsupported number of bits: %d" 208552ff457Srie #define MSG_REL_OFFSET "offset 0x%llx" 209552ff457Srie #define MSG_REL_NOFIT "value 0x%llx does not fit" 210552ff457Srie 2115aefb655Srie /* 2125aefb655Srie * Provide a macro to select the appropriate conversion routine for this 2135aefb655Srie * architecture. 2145aefb655Srie */ 2155aefb655Srie #if defined(__amd64) 2165aefb655Srie 2175aefb655Srie extern const char *conv_reloc_amd64_type(Word); 2185aefb655Srie #define CONV_RELOC_TYPE conv_reloc_amd64_type 2195aefb655Srie 2205aefb655Srie #elif defined(__i386) 2215aefb655Srie 2225aefb655Srie extern const char *conv_reloc_386_type(Word); 2235aefb655Srie #define CONV_RELOC_TYPE conv_reloc_386_type 2245aefb655Srie 2255aefb655Srie #elif defined(__sparc) 2265aefb655Srie 2275aefb655Srie extern const char *conv_reloc_SPARC_type(Word); 2285aefb655Srie #define CONV_RELOC_TYPE conv_reloc_SPARC_type 2295aefb655Srie 2305aefb655Srie #else 2315aefb655Srie #error platform not defined! 2325aefb655Srie #endif 2337c478bd9Sstevel@tonic-gate 234ba2be530Sab196087 2357c478bd9Sstevel@tonic-gate /* 2365aefb655Srie * Note: dlerror() only keeps track of a single error string, and therefore 2375aefb655Srie * must have errors reported through a single eprintf() call. The kernel's 2385aefb655Srie * _kobj_printf is somewhat more limited, and must receive messages with only 2395aefb655Srie * one argument to the format string. The following macros account for these 240f3324781Sab196087 * differences, as krtld and rtld share the same do_reloc() source. 2417c478bd9Sstevel@tonic-gate */ 2425aefb655Srie #define REL_ERR_UNIMPL(lml, file, sym, rtype) \ 243552ff457Srie _kobj_printf(ops, MSG_REL_PREFIL, (file)); \ 244552ff457Srie _kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \ 245552ff457Srie _kobj_printf(ops, MSG_REL_UNIMPL, (int)(rtype)) 246552ff457Srie 2475aefb655Srie #define REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \ 248552ff457Srie _kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \ 249552ff457Srie _kobj_printf(ops, MSG_REL_FILE, (file)); \ 250552ff457Srie _kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \ 251552ff457Srie _kobj_printf(ops, MSG_REL_UNSUPSZ, (int)(size)) 2527c478bd9Sstevel@tonic-gate 2535aefb655Srie #define REL_ERR_NONALIGN(lml, file, sym, rtype, off) \ 254552ff457Srie _kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \ 255552ff457Srie _kobj_printf(ops, MSG_REL_FILE, (file)); \ 256552ff457Srie _kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \ 257552ff457Srie _kobj_printf(ops, MSG_REL_NONALIGN, EC_OFF((off))) 2587c478bd9Sstevel@tonic-gate 2595aefb655Srie #define REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \ 260552ff457Srie _kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \ 261552ff457Srie _kobj_printf(ops, MSG_REL_FILE, (file)); \ 262552ff457Srie _kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \ 263552ff457Srie _kobj_printf(ops, MSG_REL_UNNOBITS, (nbits)) 2647c478bd9Sstevel@tonic-gate 2655aefb655Srie #define REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \ 266552ff457Srie _kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \ 267552ff457Srie _kobj_printf(ops, MSG_REL_FILE, (file)); \ 268552ff457Srie _kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \ 269552ff457Srie _kobj_printf(ops, MSG_REL_VALUE, EC_XWORD((uvalue))); \ 270552ff457Srie _kobj_printf(ops, MSG_REL_LOSEBITS, (int)(nbits)); \ 2715aefb655Srie _kobj_printf(ops, MSG_REL_OFFSET, EC_NATPTR((off))) 2727c478bd9Sstevel@tonic-gate 2735aefb655Srie #define REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \ 274552ff457Srie _kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \ 275552ff457Srie _kobj_printf(ops, MSG_REL_FILE, (file)); \ 276552ff457Srie _kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \ 277552ff457Srie _kobj_printf(ops, MSG_REL_NOFIT, EC_XWORD((uvalue))) 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate #else /* !_KERNEL */ 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate extern const char *demangle(const char *); 2837c478bd9Sstevel@tonic-gate 2845aefb655Srie #define REL_ERR_UNIMPL(lml, file, sym, rtype) \ 2855aefb655Srie (eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNIMPL), (file), \ 286552ff457Srie ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(rtype))) 287552ff457Srie 2885aefb655Srie #define REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \ 2895aefb655Srie (eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ), \ 290de777a60Sab196087 conv_reloc_type_static(M_MACH, (rtype), 0), (file), \ 291552ff457Srie ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(size))) 2927c478bd9Sstevel@tonic-gate 2935aefb655Srie #define REL_ERR_NONALIGN(lml, file, sym, rtype, off) \ 2945aefb655Srie (eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN), \ 295de777a60Sab196087 conv_reloc_type_static(M_MACH, (rtype), 0), (file), \ 296552ff457Srie ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), EC_OFF((off)))) 2977c478bd9Sstevel@tonic-gate 2985aefb655Srie #define REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \ 2995aefb655Srie (eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNNOBITS), \ 300de777a60Sab196087 conv_reloc_type_static(M_MACH, (rtype), 0), (file), \ 3017c478bd9Sstevel@tonic-gate ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (nbits))) 3027c478bd9Sstevel@tonic-gate 3035aefb655Srie #define REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \ 3045aefb655Srie (eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_LOSEBITS), \ 305de777a60Sab196087 conv_reloc_type_static(M_MACH, (rtype), 0), (file), \ 3067c478bd9Sstevel@tonic-gate ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \ 3075aefb655Srie EC_XWORD((uvalue)), (nbits), EC_NATPTR((off)))) 3087c478bd9Sstevel@tonic-gate 3095aefb655Srie #define REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \ 3105aefb655Srie (eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOFIT), \ 311de777a60Sab196087 conv_reloc_type_static(M_MACH, (rtype), 0), (file), \ 3127c478bd9Sstevel@tonic-gate ((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \ 3137c478bd9Sstevel@tonic-gate EC_XWORD((uvalue)))) 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate #ifdef __cplusplus 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate #endif 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate #endif /* _RELOC_DOT_H */ 322