#define DO_RELOC_LIBLD_SPARC
#include <string.h>
#include <stdio.h>
#include <sys/elf_SPARC.h>
#include <debug.h>
#include <reloc.h>
#include <sparc/machdep_sparc.h>
#include "msg.h"
#include "_libld.h"
#include "machsym.sparc.h"
static Sword neggotoffset = 0;
static Sword smlgotcnt = M_GOT_XNumber;
static Sword mixgotcnt = 0;
static Gotndx *
ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
{
Aliste idx;
Gotndx *gnp;
assert(rdesc != 0);
if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
return (ofl->ofl_tlsldgotndx);
for (ALIST_TRAVERSE(alp, idx, gnp)) {
if ((rdesc->rel_raddend == gnp->gn_addend) &&
(gref == gnp->gn_gotref))
return (gnp);
}
return (NULL);
}
static Xword
ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
{
Os_desc *osp = ofl->ofl_osgot;
Sym_desc *sdp = rdesc->rel_sym;
Xword gotndx;
Gotref gref;
Gotndx *gnp;
if (rdesc->rel_flags & FLG_REL_DTLS)
gref = GOT_REF_TLSGD;
else if (rdesc->rel_flags & FLG_REL_MTLS)
gref = GOT_REF_TLSLD;
else if (rdesc->rel_flags & FLG_REL_STLS)
gref = GOT_REF_TLSIE;
else
gref = GOT_REF_GENERIC;
gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
assert(gnp);
gotndx = (Xword)gnp->gn_gotndx;
if ((rdesc->rel_flags & FLG_REL_DTLS) &&
(rdesc->rel_rtype == M_R_DTPOFF))
gotndx++;
return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
(-neggotoffset * M_GOT_ENTSIZE)));
}
static Word
ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
{
Rela *rela = (Rela *)reloc;
reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info, M_MACH);
reld->rel_roffset = rela->r_offset;
reld->rel_raddend = rela->r_addend;
*typedata = (Word)ELF_R_TYPE_DATA(rela->r_info);
reld->rel_flags |= FLG_REL_RELA;
return ((Word)ELF_R_SYM(rela->r_info));
}
static void
ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
{
Word eflags = ofl->ofl_dehdr->e_flags;
Word memopt1, memopt2;
static int firstpass;
if ((ehdr->e_machine == EM_SPARC32PLUS) &&
(ehdr->e_flags & EF_SPARC_32PLUS))
ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS;
if (firstpass == 0) {
ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
firstpass++;
return;
}
eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM);
memopt1 = eflags & EF_SPARCV9_MM;
memopt2 = ehdr->e_flags & EF_SPARCV9_MM;
eflags &= ~EF_SPARCV9_MM;
if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO))
;
else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO))
eflags |= EF_SPARCV9_PSO;
else
eflags |= EF_SPARCV9_RMO;
ofl->ofl_dehdr->e_flags = eflags;
}
static void
ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
{
if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
if (ofl->ofl_pltcnt)
(*cnt)++;
}
}
static void
ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
{
if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
(*dyn)->d_tag = DT_PLTGOT;
if (ofl->ofl_osplt)
(*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr;
else
(*dyn)->d_un.d_ptr = 0;
(*dyn)++;
}
}
#if defined(_ELF64)
static Xword
ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
{
Xword value, pltndx, farpltndx;
pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
if ((pltndx) < M64_PLT_NEARPLTS) {
value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
(pltndx * M_PLT_ENTSIZE);
return (value);
}
farpltndx = pltndx - M64_PLT_NEARPLTS;
value =
(M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
((farpltndx / M64_PLT_FBLKCNTS) *
((M64_PLT_FENTSIZE + sizeof (Addr)) *
M64_PLT_FBLKCNTS)) +
((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE);
value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
return (value);
}
static uchar_t farplt_instrs[24] = {
0x8a, 0x10, 0x00, 0x0f,
0x40, 0x00, 0x00, 0x02,
0x01, 0x00, 0x00, 0x00,
0xc2, 0x5b, 0xe0, 0x00,
0x83, 0xc3, 0xc0, 0x01,
0x9e, 0x10, 0x00, 0x05
};
static void
plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
{
uint_t blockndx;
uint_t farblkcnt;
Xword farpltndx;
Xword farpltblkndx;
uint32_t *pltent;
uint64_t *pltentptr;
Sxword pltblockoff;
Sxword pltoff;
Sxword pltptroff;
uchar_t *pltbuf;
farblkcnt = ((ofl->ofl_pltcnt - 1 +
M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS);
farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS;
farpltblkndx = farpltndx % M64_PLT_FBLKCNTS;
blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS);
pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
(blockndx * M64_PLT_FBLOCKSZ);
pltoff = pltblockoff +
(farpltblkndx * M64_PLT_FENTSIZE);
pltptroff = pltblockoff;
if (farblkcnt > blockndx) {
pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) +
(farpltblkndx * M64_PLT_PSIZE);
} else {
Xword lastblkpltndx;
lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber -
M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS;
pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) +
(farpltblkndx * M64_PLT_PSIZE);
}
pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
*roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
*raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr));
pltent = (uint32_t *)(pltbuf + pltoff);
pltentptr = (uint64_t *)(pltbuf + pltptroff);
(void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs));
pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4));
*pltentptr = -(pltoff + 4);
}
static void
plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
{
uchar_t *pltent;
Sxword pltoff;
int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) {
plt_far_entry(ofl, pltndx, roffset, raddend);
return;
}
pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
*roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
*raddend = 0;
*(Word *)pltent = M_SETHIG1 | pltoff;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
pltoff += M_PLT_INSSIZE;
pltoff = -pltoff;
*(Word *)pltent = M_BA_A_XCC |
(((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19));
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_NOP;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_NOP;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_NOP;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_NOP;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_NOP;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_NOP;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
}
#else
static Xword
ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
{
Xword value, pltndx;
pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
(pltndx * M_PLT_ENTSIZE);
return (value);
}
static void
plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
{
Byte *pltent;
Sxword pltoff;
int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
*roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
*raddend = 0;
*(Word *)pltent = M_SETHIG1 | pltoff;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
pltoff += M_PLT_INSSIZE;
pltoff = -pltoff;
*(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22));
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_SETHIG0;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
pltent += M_PLT_INSSIZE;
*(Word *)pltent = M_SETHIG0;
if (bswap)
*(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
}
#endif
static uintptr_t
ld_perform_outreloc(Rel_desc *orsp, Ofl_desc *ofl, Boolean *remain_seen)
{
Os_desc *relosp, *osp = NULL;
Xword ndx, roffset, value;
Sxword raddend;
const Rel_entry *rep;
Rela rea;
char *relbits;
Sym_desc *sdp, *psym = NULL;
int sectmoved = 0;
Word dtflags1 = ofl->ofl_dtflags_1;
ofl_flag_t flags = ofl->ofl_flags;
raddend = orsp->rel_raddend;
sdp = orsp->rel_sym;
if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) {
relosp = ofl->ofl_osrel;
relbits = (char *)relosp->os_outdata->d_buf;
rea.r_info = ELF_R_INFO(0,
ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp),
orsp->rel_rtype));
rea.r_offset = orsp->rel_roffset;
rea.r_addend = raddend;
DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea,
relosp->os_name, ld_reloc_sym_name(orsp)));
assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
(void) memcpy((relbits + relosp->os_szoutrels),
(char *)&rea, sizeof (Rela));
relosp->os_szoutrels += (Xword)sizeof (Rela);
return (1);
}
if (orsp->rel_isdesc && ((orsp->rel_flags &
(FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
(orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
return (1);
}
if (RELAUX_GET_MOVE(orsp))
ld_adj_movereloc(ofl, orsp);
if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
if (ofl->ofl_parsyms &&
(sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
(psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
sectmoved = 1;
if (ofl->ofl_flags & FLG_OF_RELOBJ)
raddend = psym->sd_sym->st_value;
else
raddend = psym->sd_sym->st_value -
psym->sd_isc->is_osdesc->os_shdr->sh_addr;
raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
raddend +=
psym->sd_isc->is_osdesc->os_shdr->sh_addr;
} else {
raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
raddend +=
sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
}
}
value = sdp->sd_sym->st_value;
if (orsp->rel_flags & FLG_REL_GOT) {
osp = ofl->ofl_osgot;
roffset = ld_calc_got_offset(orsp, ofl);
} else if (orsp->rel_flags & FLG_REL_PLT) {
osp = ofl->ofl_osplt;
plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend);
} else if (orsp->rel_flags & FLG_REL_BSS) {
osp = ofl->ofl_isbss->is_osdesc;
roffset = (Xword)value;
raddend = 0;
} else if (orsp->rel_flags & FLG_REL_REG) {
roffset = orsp->rel_roffset;
} else {
osp = RELAUX_GET_OSDESC(orsp);
roffset = orsp->rel_roffset +
(Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
roffset += orsp->rel_isdesc->is_osdesc->
os_shdr->sh_addr;
}
if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
relosp = ofl->ofl_osrel;
rep = &reloc_table[orsp->rel_rtype];
if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) &&
!(rep->re_flags & FLG_RE_UNALIGN)) {
if (((rep->re_fsize == 2) && (roffset & 0x1)) ||
((rep->re_fsize == 4) && (roffset & 0x3)) ||
((rep->re_fsize == 8) && (roffset & 0x7))) {
Conv_inv_buf_t inv_buf;
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN),
conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf),
orsp->rel_isdesc->is_file->ifl_name,
ld_reloc_sym_name(orsp), EC_XWORD(roffset));
return (S_ERROR);
}
}
if (orsp->rel_rtype == R_SPARC_RELATIVE)
ndx = STN_UNDEF;
else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
(ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
if (sectmoved == 0) {
if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
ndx = sdp->sd_isc->is_osdesc->os_identndx;
else
ndx = sdp->sd_shndx;
} else
ndx = ofl->ofl_parexpnndx;
} else
ndx = sdp->sd_symndx;
if (orsp->rel_flags & FLG_REL_ADVAL)
raddend += value;
if (orsp->rel_rtype == M_R_DTPMOD)
raddend = 0;
if ((orsp->rel_rtype != M_R_NONE) &&
(orsp->rel_rtype != M_R_REGISTER) &&
(orsp->rel_rtype != M_R_RELATIVE)) {
if (ndx == 0) {
Conv_inv_buf_t inv_buf;
Is_desc *isp = orsp->rel_isdesc;
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
conv_reloc_type(ofl->ofl_nehdr->e_machine,
orsp->rel_rtype, 0, &inv_buf),
isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
isp->is_name, EC_XWORD(roffset));
return (S_ERROR);
}
}
rea.r_info = ELF_R_INFO(ndx,
ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype));
rea.r_offset = roffset;
rea.r_addend = raddend;
DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
ld_reloc_sym_name(orsp)));
assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
relbits = (char *)relosp->os_outdata->d_buf;
(void) memcpy((relbits + relosp->os_szoutrels),
(char *)&rea, sizeof (Rela));
relosp->os_szoutrels += (Xword)sizeof (Rela);
ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
return (1);
}
#if defined(_ELF64)
#define TLS_GD_IE_LD 0xd0580000
#else
#define TLS_GD_IE_LD 0xd0000000
#endif
#define TLS_GD_IE_ADD 0x9001c008
#define TLS_GD_LE_XOR 0x80182000
#define TLS_IE_LE_OR 0x80100000
#define TLS_LD_LE_CLRO0 0x90100000
#define FM3_REG_MSK_RD (0x1f << 25)
#define FM3_REG_MSK_RS1 (0x1f << 14)
#define FM3_REG_MSK_RS2 0x1f
#define REG_G7 7
static Fixupret
tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
{
Sym_desc *sdp = arsp->rel_sym;
Word rtype = arsp->rel_rtype;
Word *offset, w;
int bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
offset = (Word *)((uintptr_t)arsp->rel_roffset +
(uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
(uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
if (sdp->sd_ref == REF_DYN_NEED) {
switch (rtype) {
case R_SPARC_TLS_GD_HI22:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_TLS_IE_HI22, arsp,
ld_reloc_sym_name));
arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
return (FIX_RELOC);
case R_SPARC_TLS_GD_LO10:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_TLS_IE_LO10, arsp,
ld_reloc_sym_name));
arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
return (FIX_RELOC);
case R_SPARC_TLS_GD_ADD:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
w = bswap ? ld_bswap_Word(*offset) : *offset;
w = (TLS_GD_IE_LD |
(w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
*offset = bswap ? ld_bswap_Word(w) : w;
return (FIX_DONE);
case R_SPARC_TLS_GD_CALL:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
*offset = TLS_GD_IE_ADD;
if (bswap)
*offset = ld_bswap_Word(*offset);
return (FIX_DONE);
}
return (FIX_RELOC);
}
switch (rtype) {
case R_SPARC_TLS_IE_HI22:
case R_SPARC_TLS_GD_HI22:
case R_SPARC_TLS_LDO_HIX22:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name));
arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
return (FIX_RELOC);
case R_SPARC_TLS_LDO_LOX10:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
return (FIX_RELOC);
case R_SPARC_TLS_IE_LO10:
case R_SPARC_TLS_GD_LO10:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
w = bswap ? ld_bswap_Word(*offset) : *offset;
w = TLS_GD_LE_XOR |
(w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
*offset = bswap ? ld_bswap_Word(w) : w;
arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
return (FIX_RELOC);
case R_SPARC_TLS_IE_LD:
case R_SPARC_TLS_IE_LDX:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
w = bswap ? ld_bswap_Word(*offset) : *offset;
w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR;
*offset = bswap ? ld_bswap_Word(w) : w;
return (FIX_DONE);
case R_SPARC_TLS_LDO_ADD:
case R_SPARC_TLS_GD_ADD:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
w = bswap ? ld_bswap_Word(*offset) : *offset;
w = w & (~FM3_REG_MSK_RS1);
w = w | (REG_G7 << 14);
*offset = bswap ? ld_bswap_Word(w) : w;
return (FIX_DONE);
case R_SPARC_TLS_LDM_CALL:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
*offset = TLS_LD_LE_CLRO0;
if (bswap)
*offset = ld_bswap_Word(*offset);
return (FIX_DONE);
case R_SPARC_TLS_LDM_HI22:
case R_SPARC_TLS_LDM_LO10:
case R_SPARC_TLS_LDM_ADD:
case R_SPARC_TLS_IE_ADD:
case R_SPARC_TLS_GD_CALL:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
*offset = M_NOP;
if (bswap)
*offset = ld_bswap_Word(*offset);
return (FIX_DONE);
}
return (FIX_RELOC);
}
#define GOTOP_ADDINST 0x80000000
static Fixupret
gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
{
Word rtype = arsp->rel_rtype;
Word *offset, w;
const char *ifl_name;
Conv_inv_buf_t inv_buf;
int bswap;
switch (rtype) {
case R_SPARC_GOTDATA_OP_HIX22:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name));
arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
return (FIX_RELOC);
case R_SPARC_GOTDATA_OP_LOX10:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name));
arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
return (FIX_RELOC);
case R_SPARC_GOTDATA_OP:
DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
R_SPARC_NONE, arsp, ld_reloc_sym_name));
offset = (Word *)(uintptr_t)(arsp->rel_roffset +
_elf_getxoff(arsp->rel_isdesc->is_indata) +
(uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
w = bswap ? ld_bswap_Word(*offset) : *offset;
w = (w & (FM3_REG_MSK_RS1 |
FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
*offset = bswap ? ld_bswap_Word(w) : w;
return (FIX_DONE);
}
if (arsp->rel_isdesc->is_file)
ifl_name = arsp->rel_isdesc->is_file->ifl_name;
else
ifl_name = MSG_INTL(MSG_STR_NULL);
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
ifl_name, ld_reloc_sym_name(arsp));
assert(0);
return (FIX_ERROR);
}
static uintptr_t
ld_do_activerelocs(Ofl_desc *ofl)
{
Rel_desc *arsp;
Rel_cachebuf *rcbp;
Aliste idx;
uintptr_t return_code = 1;
ofl_flag_t flags = ofl->ofl_flags;
if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
uchar_t *addr;
Xword value;
Sym_desc *sdp;
const char *ifl_name;
Xword refaddr;
Os_desc *osp;
if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
continue;
}
if (arsp->rel_flags & FLG_REL_TLSFIX) {
Fixupret ret;
if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
return (S_ERROR);
if (ret == FIX_DONE)
continue;
}
if (arsp->rel_flags & FLG_REL_GOTFIX) {
Fixupret ret;
if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR)
return (S_ERROR);
if (ret == FIX_DONE)
continue;
}
if (RELAUX_GET_MOVE(arsp))
ld_adj_movereloc(ofl, arsp);
sdp = arsp->rel_sym;
refaddr = arsp->rel_roffset +
(Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
if ((arsp->rel_flags & FLG_REL_CLVAL) ||
(arsp->rel_flags & FLG_REL_GOTCL))
value = 0;
else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
Sym_desc *sym;
if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
(sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
value = _elf_getxoff(sym->sd_isc->is_indata);
if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
value += sym->sd_isc->
is_osdesc->os_shdr->sh_addr;
arsp->rel_raddend -= sym->sd_osym->st_value;
} else {
value = _elf_getxoff(sdp->sd_isc->is_indata);
if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
value += sdp->sd_isc->
is_osdesc->os_shdr->sh_addr;
}
if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
value -= ofl->ofl_tlsphdr->p_vaddr;
} else if (IS_SIZE(arsp->rel_rtype)) {
value = sdp->sd_sym->st_size;
} else if ((sdp->sd_flags & FLG_SY_CAP) &&
sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
value = ld_calc_plt_addr(sdp, ofl);
} else
value = sdp->sd_sym->st_value;
if ((arsp->rel_flags & FLG_REL_GOT) &&
!ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
return (S_ERROR);
osp = RELAUX_GET_OSDESC(arsp);
if ((arsp->rel_flags & FLG_REL_LOAD) &&
((flags & FLG_OF_RELOBJ) == 0))
refaddr +=
arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
if (IS_PLT(arsp->rel_rtype)) {
if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
value = ld_calc_plt_addr(sdp, ofl);
}
value += arsp->rel_raddend;
if (IS_EXTOFFSET(arsp->rel_rtype))
value += RELAUX_GET_TYPEDATA(arsp);
if ((arsp->rel_flags & FLG_REL_GOT) &&
((flags & FLG_OF_RELOBJ) == 0)) {
Xword R1addr;
uintptr_t R2addr;
Sword gotndx;
Gotndx *gnp;
Gotref gref;
if (arsp->rel_flags & FLG_REL_DTLS)
gref = GOT_REF_TLSGD;
else if (arsp->rel_flags & FLG_REL_MTLS)
gref = GOT_REF_TLSLD;
else if (arsp->rel_flags & FLG_REL_STLS)
gref = GOT_REF_TLSIE;
else
gref = GOT_REF_GENERIC;
gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
assert(gnp);
if (arsp->rel_rtype == M_R_DTPOFF)
gotndx = gnp->gn_gotndx + 1;
else
gotndx = gnp->gn_gotndx;
R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) +
(gotndx * M_GOT_ENTSIZE));
R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
arsp, R1addr, value, ld_reloc_sym_name));
if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
*(Xword *)R2addr = ld_bswap_Xword(value);
else
*(Xword *)R2addr = value;
continue;
} else if (IS_GOT_BASED(arsp->rel_rtype) &&
((flags & FLG_OF_RELOBJ) == 0)) {
value -= (ofl->ofl_osgot->os_shdr->sh_addr +
(-neggotoffset * M_GOT_ENTSIZE));
} else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
value -= refaddr;
} else if (IS_TLS_INS(arsp->rel_rtype) &&
IS_GOT_RELATIVE(arsp->rel_rtype) &&
((flags & FLG_OF_RELOBJ) == 0)) {
Gotndx *gnp;
Gotref gref;
if (arsp->rel_flags & FLG_REL_STLS)
gref = GOT_REF_TLSIE;
else if (arsp->rel_flags & FLG_REL_DTLS)
gref = GOT_REF_TLSGD;
else if (arsp->rel_flags & FLG_REL_MTLS)
gref = GOT_REF_TLSLD;
gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
assert(gnp);
value = gnp->gn_gotndx * M_GOT_ENTSIZE;
} else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
((flags & FLG_OF_RELOBJ) == 0)) {
Gotndx *gnp;
gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
GOT_REF_GENERIC, ofl, arsp);
assert(gnp);
value = gnp->gn_gotndx * M_GOT_ENTSIZE;
} else if ((arsp->rel_flags & FLG_REL_STLS) &&
((flags & FLG_OF_RELOBJ) == 0)) {
Xword tlsstatsize;
tlsstatsize =
S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
value = -(tlsstatsize - value);
}
if (arsp->rel_isdesc->is_file)
ifl_name = arsp->rel_isdesc->is_file->ifl_name;
else
ifl_name = MSG_INTL(MSG_STR_NULL);
if (arsp->rel_isdesc->is_indata->d_buf == 0) {
Conv_inv_buf_t inv_buf;
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
ifl_name, ld_reloc_sym_name(arsp),
EC_WORD(arsp->rel_isdesc->is_scnndx),
arsp->rel_isdesc->is_name);
return (S_ERROR);
}
addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
(uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
ld_reloc_sym_name));
addr += (uintptr_t)osp->os_outdata->d_buf;
if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
ofl->ofl_size) || (arsp->rel_roffset >
osp->os_shdr->sh_size)) {
Conv_inv_buf_t inv_buf;
int class;
if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
ofl->ofl_size)
class = ERR_FATAL;
else
class = ERR_WARNING;
ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
EC_ADDR((uintptr_t)addr -
(uintptr_t)ofl->ofl_nehdr));
if (class == ERR_FATAL) {
return_code = S_ERROR;
continue;
}
}
if (OFL_DO_RELOC(ofl)) {
if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
ofl->ofl_lml) == 0) {
ofl->ofl_flags |= FLG_OF_FATAL;
return_code = S_ERROR;
}
}
}
return (return_code);
}
static uintptr_t
ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
{
Rel_desc *orsp;
Sym_desc *sdp = rsp->rel_sym;
Conv_inv_buf_t inv_buf;
if (OFL_IS_STATIC_EXEC(ofl))
return (1);
if ((sdp != NULL) &&
(sdp->sd_sym->st_shndx == SHN_UNDEF) &&
(rsp->rel_rtype != M_R_NONE) &&
(rsp->rel_rtype != M_R_REGISTER) &&
(rsp->rel_rtype != M_R_RELATIVE)) {
if (ld_sym_reducable(ofl, sdp))
return (1);
}
if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
Word rtype = rsp->rel_rtype;
if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNRELREL),
conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
rsp->rel_isdesc->is_file->ifl_name,
ld_reloc_sym_name(rsp));
return (S_ERROR);
}
#if defined(_ELF64)
if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
(rtype == R_SPARC_L44)) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SHOBJABS44),
conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
rsp->rel_isdesc->is_file->ifl_name,
ld_reloc_sym_name(rsp));
return (S_ERROR);
}
#endif
}
if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
((flags & FLG_REL_SCNNDX) ||
(ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
(sdp->sd_sym->st_shndx == SHN_COMMON)) {
if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
else
ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
} else {
Os_desc *osp;
Is_desc *isp = sdp->sd_isc;
if (isp && ((osp = isp->is_osdesc) != NULL) &&
((osp->os_flags & FLG_OS_OUTREL) == 0)) {
ofl->ofl_dynshdrcnt++;
osp->os_flags |= FLG_OS_OUTREL;
}
}
}
if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
return (S_ERROR);
if (flags & FLG_REL_GOT)
ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
else if (flags & FLG_REL_PLT)
ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
else if (flags & FLG_REL_BSS)
ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
else if (flags & FLG_REL_NOINFO)
ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
else
RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
if (orsp->rel_rtype == M_R_RELATIVE)
ofl->ofl_relocrelcnt++;
#if defined(_ELF64)
if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
ofl->ofl_pltpad++;
orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
}
#endif
if (!(flags & FLG_REL_PLT))
ofl->ofl_reloccnt++;
if (IS_GOT_REQUIRED(orsp->rel_rtype))
ofl->ofl_flags |= FLG_OF_BLDGOT;
if (orsp->rel_flags & FLG_REL_DISP) {
ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
if (ofl->ofl_flags & FLG_OF_VERBOSE)
ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
}
DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
M_MACH, orsp));
return (1);
}
static uintptr_t
ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl)
{
if (ofl->ofl_flags & FLG_OF_MULDEFS) {
Ifl_desc *ifl = isp->is_file;
Sym_desc *sdp = rsp->rel_sym;
if (sdp == 0) {
Xword offset = rsp->rel_roffset;
Word ndx;
for (ndx = ifl->ifl_locscnt;
ndx < ifl->ifl_symscnt; ndx++) {
if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
(sdp->sd_flags & FLG_SY_REGSYM) &&
(sdp->sd_sym->st_value == offset))
break;
}
}
if (sdp && (sdp->sd_file != ifl))
return (1);
}
return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
}
static uintptr_t
ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl)
{
ofl_flag_t flags = ofl->ofl_flags;
Sym_desc *sdp = rsp->rel_sym;
Word shndx = sdp->sd_sym->st_shndx;
if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
!(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
!(IS_GOT_BASED(rsp->rel_rtype)) &&
!(rsp->rel_isdesc != NULL &&
(rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
(((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
(shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
Word ortype = rsp->rel_rtype;
if ((rsp->rel_rtype != R_SPARC_32) &&
(rsp->rel_rtype != R_SPARC_PLT32) &&
(rsp->rel_rtype != R_SPARC_64))
return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
rsp, ofl));
rsp->rel_rtype = R_SPARC_RELATIVE;
if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
return (S_ERROR);
rsp->rel_rtype = ortype;
return (1);
}
if (!(rsp->rel_flags & FLG_REL_LOAD) &&
((shndx == SHN_UNDEF) ||
((sdp->sd_ref == REF_DYN_NEED) &&
((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
Conv_inv_buf_t inv_buf;
Os_desc *osp = RELAUX_GET_OSDESC(rsp);
if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
return (0);
ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
rsp->rel_isdesc->is_file->ifl_name,
ld_reloc_sym_name(rsp), osp->os_name);
return (1);
}
return (ld_add_actrel(0, rsp, ofl));
}
static uintptr_t
ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
{
Word rtype = rsp->rel_rtype;
if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
if (rtype == R_SPARC_GOTDATA_OP)
return (1);
return (ld_reloc_GOT_relative(local, rsp, ofl));
}
return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
}
static uintptr_t
ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
{
Word rtype = rsp->rel_rtype;
Sym_desc *sdp = rsp->rel_sym;
ofl_flag_t flags = ofl->ofl_flags;
Gotndx *gnp;
if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
ofl->ofl_dtflags |= DF_STATIC_TLS;
if (!local || ((flags & FLG_OF_EXEC) == 0)) {
if ((rtype == R_SPARC_TLS_IE_LD) ||
(rtype == R_SPARC_TLS_IE_LDX) ||
(rtype == R_SPARC_TLS_IE_ADD))
return (1);
if (((rtype == R_SPARC_TLS_GD_HI22) ||
(rtype == R_SPARC_TLS_GD_LO10) ||
(rtype == R_SPARC_TLS_IE_HI22) ||
(rtype == R_SPARC_TLS_IE_LO10)) &&
((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
GOT_REF_TLSIE, ofl, rsp)) == NULL)) {
if (ld_assign_got_TLS(local, rsp, ofl, sdp,
gnp, GOT_REF_TLSIE, FLG_REL_STLS,
rtype, M_R_TPOFF, 0) == S_ERROR)
return (S_ERROR);
}
if (IS_TLS_IE(rtype))
return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
rsp, ofl));
}
if (IS_TLS_LE(rtype))
return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
if (rtype == R_SPARC_TLS_IE_ADD)
return (1);
return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
rsp, ofl));
}
if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
(rtype == R_SPARC_TLS_LDO_ADD))
return (1);
if (((rtype == R_SPARC_TLS_LDM_HI22) ||
(rtype == R_SPARC_TLS_LDM_LO10)) &&
((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD,
ofl, rsp)) == NULL)) {
if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
return (S_ERROR);
} else if (((rtype == R_SPARC_TLS_GD_HI22) ||
(rtype == R_SPARC_TLS_GD_LO10)) &&
((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
ofl, rsp)) == NULL)) {
if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
return (S_ERROR);
}
if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
Sym_desc *tlsgetsym;
if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
return (S_ERROR);
rsp->rel_sym = tlsgetsym;
rsp->rel_rtype = R_SPARC_WPLT30;
if (ld_reloc_plt(rsp, ofl) == S_ERROR)
return (S_ERROR);
rsp->rel_sym = sdp;
rsp->rel_rtype = rtype;
return (1);
}
if (IS_TLS_LD(rtype))
return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
}
static Sword small_index;
static Sword mixed_index;
static Sword large_index;
static uintptr_t
ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp)
{
Aliste idx;
Gotndx *gnp;
for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) {
uint_t gotents;
Gotref gref = gnp->gn_gotref;
if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
gotents = 2;
else
gotents = 1;
switch (gnp->gn_gotndx) {
case M_GOT_SMALL:
gnp->gn_gotndx = small_index;
small_index += gotents;
if (small_index == 0)
small_index = M_GOT_XNumber;
break;
case M_GOT_MIXED:
gnp->gn_gotndx = mixed_index;
mixed_index += gotents;
break;
case M_GOT_LARGE:
gnp->gn_gotndx = large_index;
large_index += gotents;
break;
default:
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_ASSIGNGOT),
EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
return (S_ERROR);
}
}
return (1);
}
static uintptr_t
ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
Rel_desc *rsp, Sym_desc *sdp)
{
Xword raddend;
Gotndx gn, *gnp;
Aliste idx;
uint_t gotents;
if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
gotents = 2;
else
gotents = 1;
raddend = rsp->rel_raddend;
if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
switch (pgnp->gn_gotndx) {
case M_GOT_SMALL:
if (rsp->rel_rtype != R_SPARC_GOT13) {
pgnp->gn_gotndx = M_GOT_MIXED;
mixgotcnt += gotents;
}
break;
case M_GOT_LARGE:
if (rsp->rel_rtype == R_SPARC_GOT13) {
smlgotcnt += gotents;
mixgotcnt += gotents;
pgnp->gn_gotndx = M_GOT_MIXED;
sdp->sd_flags |= FLG_SY_SMGOT;
}
break;
}
return (1);
}
gn.gn_addend = raddend;
gn.gn_gotref = gref;
if (rsp->rel_rtype == R_SPARC_GOT13) {
gn.gn_gotndx = M_GOT_SMALL;
smlgotcnt += gotents;
sdp->sd_flags |= FLG_SY_SMGOT;
} else
gn.gn_gotndx = M_GOT_LARGE;
ofl->ofl_gotcnt += gotents;
if (gref == GOT_REF_TLSLD) {
if (ofl->ofl_tlsldgotndx == NULL) {
if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
return (S_ERROR);
(void) memcpy(gnp, &gn, sizeof (Gotndx));
ofl->ofl_tlsldgotndx = gnp;
}
return (1);
}
idx = 0;
for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
if (gnp->gn_addend > raddend)
break;
}
if (alist_insert(alpp, &gn, sizeof (Gotndx),
AL_CNT_SDP_GOT, idx) == NULL)
return (S_ERROR);
return (1);
}
static void
ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
{
sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
}
static uintptr_t
ld_allocate_got(Ofl_desc * ofl)
{
const Sword first_large_ndx = M_GOT_MAXSMALL / 2;
Sym_desc *sdp;
Addr addr;
if (smlgotcnt > M_GOT_MAXSMALL) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
return (S_ERROR);
}
if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
return (S_ERROR);
}
neggotoffset = ((smlgotcnt >= first_large_ndx) ?
(first_large_ndx - smlgotcnt) : 0);
small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
large_index = neggotoffset + smlgotcnt;
mixed_index = large_index - mixgotcnt;
addr = -neggotoffset * M_GOT_ENTSIZE;
if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH,
NULL, ofl)) != NULL)
sdp->sd_sym->st_value = addr;
if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH,
NULL, ofl)) != NULL)
sdp->sd_sym->st_value = addr;
if (ofl->ofl_tlsldgotndx) {
ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
large_index += 2;
}
return (1);
}
static uintptr_t
ld_fillin_gotplt(Ofl_desc *ofl)
{
if (ofl->ofl_osgot) {
Sym_desc *sdp;
if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
SYM_NOHASH, NULL, ofl)) != NULL) {
uchar_t *genptr;
genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
(-neggotoffset * M_GOT_ENTSIZE) +
(M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
*((Xword *)genptr) = sdp->sd_sym->st_value;
if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
*((Xword *)genptr) =
ld_bswap_Xword(*((Xword *)genptr));
}
}
return (1);
}
static const uchar_t nullfunc_tmpl[] = {
0x81, 0xc3, 0xe0, 0x08,
0x01, 0x00, 0x00, 0x00
};
const Target *
ld_targ_init_sparc(void)
{
static const Target _ld_targ = {
{
M_MACH,
M_MACHPLUS,
M_FLAGSPLUS,
M_CLASS,
M_DATA,
M_SEGM_ALIGN,
M_SEGM_ORIGIN,
M_SEGM_AORIGIN,
M_DATASEG_PERM,
M_STACK_PERM,
M_WORD_ALIGN,
#if defined(_ELF64)
MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
#else
MSG_ORIG(MSG_PTH_RTLD),
#endif
M_R_ARRAYADDR,
M_R_COPY,
M_R_GLOB_DAT,
M_R_JMP_SLOT,
M_R_NUM,
M_R_NONE,
M_R_RELATIVE,
M_R_REGISTER,
M_REL_DT_COUNT,
M_REL_DT_ENT,
M_REL_DT_SIZE,
M_REL_DT_TYPE,
M_REL_SHT_TYPE,
M_GOT_ENTSIZE,
M_GOT_XNumber,
M_PLT_ALIGN,
M_PLT_ENTSIZE,
M_PLT_RESERVSZ,
M_PLT_SHF_FLAGS,
SHT_PROGBITS,
M_DT_REGISTER,
},
{
M_ID_ARRAY,
M_ID_BSS,
M_ID_CAP,
M_ID_CAPINFO,
M_ID_CAPCHAIN,
M_ID_DATA,
M_ID_DYNAMIC,
M_ID_DYNSORT,
M_ID_DYNSTR,
M_ID_DYNSYM,
M_ID_DYNSYM_NDX,
M_ID_GOT,
M_ID_GOTDATA,
M_ID_HASH,
M_ID_INTERP,
M_ID_UNKNOWN,
M_ID_LDYNSYM,
M_ID_NOTE,
M_ID_NULL,
M_ID_PLT,
M_ID_REL,
M_ID_STRTAB,
M_ID_SYMINFO,
M_ID_SYMTAB,
M_ID_SYMTAB_NDX,
M_ID_TEXT,
M_ID_TLS,
M_ID_TLSBSS,
M_ID_UNKNOWN,
M_ID_UNWIND,
M_ID_UNWINDHDR,
M_ID_USER,
M_ID_VERSION,
},
{
nullfunc_tmpl,
sizeof (nullfunc_tmpl),
},
{
NULL
},
{
reloc_table,
ld_init_rel,
ld_mach_eflags,
ld_mach_make_dynamic,
ld_mach_update_odynamic,
ld_calc_plt_addr,
ld_perform_outreloc,
ld_do_activerelocs,
ld_add_outrel,
ld_reloc_register,
ld_reloc_local,
ld_reloc_GOTOP,
ld_reloc_TLS,
ld_assign_got,
ld_find_got_ndx,
ld_calc_got_offset,
ld_assign_got_ndx,
ld_assign_plt_ndx,
ld_allocate_got,
ld_fillin_gotplt,
},
{
ld_reg_check_sparc,
ld_mach_sym_typecheck_sparc,
ld_is_regsym_sparc,
ld_reg_find_sparc,
ld_reg_enter_sparc
}
};
return (&_ld_targ);
}