#ifndef _RELOC_DOT_H
#define _RELOC_DOT_H
#if defined(_KERNEL)
#include <sys/bootconf.h>
#include <sys/kobj.h>
#include <sys/kobj_impl.h>
#else
#include <rtld.h>
#include <conv.h>
#endif
#include "reloc_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(_KERNEL)
#if defined(DO_RELOC_LIBLD)
#undef DO_RELOC_LIBLD
#endif
#if defined(DO_RELOC_LIBLD_X86)
#define DO_RELOC_LIBLD
#if defined(_ELF64)
#define do_reloc_ld do64_reloc_ld_x86
#define reloc_table reloc64_table_x86
#else
#define do_reloc_ld do32_reloc_ld_x86
#define reloc_table reloc32_table_x86
#endif
#elif defined(DO_RELOC_LIBLD_SPARC)
#define DO_RELOC_LIBLD
#if defined(_ELF64)
#define do_reloc_ld do64_reloc_ld_sparc
#define reloc_table reloc64_table_sparc
#else
#define do_reloc_ld do32_reloc_ld_sparc
#define reloc_table reloc32_table_sparc
#endif
#else
#if defined(_ELF64)
#define do_reloc_rtld do64_reloc_rtld
#define reloc_table reloc64_table
#else
#define do_reloc_rtld do32_reloc_rtld
#define reloc_table reloc32_table
#endif
#endif
#endif
extern const Rel_entry reloc_table[];
#define IS_PLT(X) RELTAB_IS_PLT(X, reloc_table)
#define IS_GOT_RELATIVE(X) RELTAB_IS_GOT_RELATIVE(X, reloc_table)
#define IS_GOT_PC(X) RELTAB_IS_GOT_PC(X, reloc_table)
#define IS_GOTPCREL(X) RELTAB_IS_GOTPCREL(X, reloc_table)
#define IS_GOT_BASED(X) RELTAB_IS_GOT_BASED(X, reloc_table)
#define IS_GOT_OPINS(X) RELTAB_IS_GOT_OPINS(X, reloc_table)
#define IS_GOT_REQUIRED(X) RELTAB_IS_GOT_REQUIRED(X, reloc_table)
#define IS_PC_RELATIVE(X) RELTAB_IS_PC_RELATIVE(X, reloc_table)
#define IS_ADD_RELATIVE(X) RELTAB_IS_ADD_RELATIVE(X, reloc_table)
#define IS_REGISTER(X) RELTAB_IS_REGISTER(X, reloc_table)
#define IS_NOTSUP(X) RELTAB_IS_NOTSUP(X, reloc_table)
#define IS_SEG_RELATIVE(X) RELTAB_IS_SEG_RELATIVE(X, reloc_table)
#define IS_EXTOFFSET(X) RELTAB_IS_EXTOFFSET(X, reloc_table)
#define IS_SEC_RELATIVE(X) RELTAB_IS_SEC_RELATIVE(X, reloc_table)
#define IS_TLS_INS(X) RELTAB_IS_TLS_INS(X, reloc_table)
#define IS_TLS_GD(X) RELTAB_IS_TLS_GD(X, reloc_table)
#define IS_TLS_LD(X) RELTAB_IS_TLS_LD(X, reloc_table)
#define IS_TLS_IE(X) RELTAB_IS_TLS_IE(X, reloc_table)
#define IS_TLS_LE(X) RELTAB_IS_TLS_LE(X, reloc_table)
#define IS_LOCALBND(X) RELTAB_IS_LOCALBND(X, reloc_table)
#define IS_SIZE(X) RELTAB_IS_SIZE(X, reloc_table)
#if defined(_KERNEL)
extern int do_reloc_krtld(uchar_t, uchar_t *, Xword *, const char *,
const char *);
#elif defined(DO_RELOC_LIBLD)
extern int do_reloc_ld(Rel_desc *, uchar_t *, Xword *,
rel_desc_sname_func_t, const char *, int, void *);
#else
extern int do_reloc_rtld(uchar_t, uchar_t *, Xword *, const char *,
const char *, void *);
#endif
#if defined(_KERNEL)
#define S_MASK(n) ((1l << (n)) - 1l)
#define S_INRANGE(v, n) (((-(1l << (n)) - 1l) < (v)) && ((v) < (1l << (n))))
#define MSG_STR_UNKNOWN "(unknown)"
#define MSG_REL_PREGEN "relocation error: %s: "
#define MSG_REL_PREFIL "relocation error: file %s: "
#define MSG_REL_FILE "file %s: "
#define MSG_REL_SYM "symbol %s: "
#define MSG_REL_VALUE "value 0x%llx "
#define MSG_REL_LOSEBITS "loses %d bits at "
#define MSG_REL_UNIMPL "unimplemented relocation type: %d"
#define MSG_REL_UNSUPSZ "offset size (%d bytes) is not supported"
#define MSG_REL_NONALIGN "offset 0x%llx is non-aligned"
#define MSG_REL_UNNOBITS "unsupported number of bits: %d"
#define MSG_REL_OFFSET "offset 0x%llx"
#define MSG_REL_NOFIT "value 0x%llx does not fit"
#if defined(__amd64)
extern const char *conv_reloc_amd64_type(Word);
#define CONV_RELOC_TYPE conv_reloc_amd64_type
#elif defined(__i386)
extern const char *conv_reloc_386_type(Word);
#define CONV_RELOC_TYPE conv_reloc_386_type
#elif defined(__sparc)
extern const char *conv_reloc_SPARC_type(Word);
#define CONV_RELOC_TYPE conv_reloc_SPARC_type
#else
#error platform not defined!
#endif
#define REL_ERR_UNIMPL(lml, file, sym, rtype) \
_kobj_printf(ops, MSG_REL_PREFIL, (file)); \
_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
_kobj_printf(ops, MSG_REL_UNIMPL, (int)(rtype))
#define REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \
_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
_kobj_printf(ops, MSG_REL_FILE, (file)); \
_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
_kobj_printf(ops, MSG_REL_UNSUPSZ, (int)(size))
#define REL_ERR_NONALIGN(lml, file, sym, rtype, off) \
_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
_kobj_printf(ops, MSG_REL_FILE, (file)); \
_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
_kobj_printf(ops, MSG_REL_NONALIGN, (u_longlong_t)EC_OFF((off)))
#define REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \
_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
_kobj_printf(ops, MSG_REL_FILE, (file)); \
_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
_kobj_printf(ops, MSG_REL_UNNOBITS, (nbits))
#define REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \
_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
_kobj_printf(ops, MSG_REL_FILE, (file)); \
_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
_kobj_printf(ops, MSG_REL_VALUE, (u_longlong_t)EC_XWORD((uvalue))); \
_kobj_printf(ops, MSG_REL_LOSEBITS, (int)(nbits)); \
_kobj_printf(ops, MSG_REL_OFFSET, (u_longlong_t)EC_NATPTR((off)))
#define REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \
_kobj_printf(ops, MSG_REL_PREGEN, CONV_RELOC_TYPE((rtype))); \
_kobj_printf(ops, MSG_REL_FILE, (file)); \
_kobj_printf(ops, MSG_REL_SYM, ((sym) ? (sym) : MSG_STR_UNKNOWN)); \
_kobj_printf(ops, MSG_REL_NOFIT, (u_longlong_t)EC_XWORD((uvalue)))
#else
extern const char *demangle(const char *);
#define REL_ERR_UNIMPL(lml, file, sym, rtype) \
(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNIMPL), (file), \
((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(rtype)))
#define REL_ERR_UNSUPSZ(lml, file, sym, rtype, size) \
(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ), \
conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (int)(size)))
#define REL_ERR_NONALIGN(lml, file, sym, rtype, off) \
(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN), \
conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), EC_OFF((off))))
#define REL_ERR_UNNOBITS(lml, file, sym, rtype, nbits) \
(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_UNNOBITS), \
conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), (nbits)))
#define REL_ERR_LOSEBITS(lml, file, sym, rtype, uvalue, nbits, off) \
(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_LOSEBITS), \
conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
EC_XWORD((uvalue)), (nbits), EC_NATPTR((off))))
#define REL_ERR_NOFIT(lml, file, sym, rtype, uvalue) \
(eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOFIT), \
conv_reloc_type_static(M_MACH, (rtype), 0), (file), \
((sym) ? demangle(sym) : MSG_INTL(MSG_STR_UNKNOWN)), \
EC_XWORD((uvalue))))
#endif
#ifdef __cplusplus
}
#endif
#endif