#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <synch.h>
#include <signal.h>
#include <synch.h>
#include <link.h>
#include <libintl.h>
#include <sys/param.h>
#include <procfs.h>
#include "msg.h"
#include "sgs.h"
#include "profile.h"
#include "_rtld.h"
static char Profile[MAXPATHLEN];
static char *pname = 0;
static L_hdr *Hptr;
static L_cgarc *Cptr;
static caddr_t Hpc, Lpc;
static size_t Fsize;
uintptr_t profcookie = 0;
static const sigset_t iset = { ~0U, ~0U, ~0U, ~0U };
static lwp_mutex_t sharedmutex = SHAREDMUTEX;
static int
prof_mutex_init(lwp_mutex_t *mp)
{
(void) memcpy(mp, &sharedmutex, sizeof (lwp_mutex_t));
return (0);
}
static int
prof_mutex_lock(lwp_mutex_t *mp, sigset_t *oset)
{
if (oset)
(void) sigprocmask(SIG_BLOCK, &iset, oset);
(void) _lwp_mutex_lock(mp);
return (0);
}
static int
prof_mutex_unlock(mutex_t *mp, sigset_t *oset)
{
(void) _lwp_mutex_unlock(mp);
if (oset)
(void) sigprocmask(SIG_SETMASK, oset, NULL);
return (0);
}
const char *
_ldprof_msg(Msg mid)
{
return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
}
static int
setisinuse(uint32_t *sp, uint_t n)
{
while (n--)
if (*sp++)
return (1);
return (0);
}
#define prisinuse(sp) \
setisinuse((uint32_t *)(sp), \
(uint_t)(sizeof (*(sp)) / sizeof (uint32_t)))
uint_t
la_version(uint_t version)
{
int fd;
ssize_t num;
pstatus_t status;
if (version < LAV_CURRENT) {
(void) fprintf(stderr, MSG_INTL(MSG_GEN_AUDITVERSION),
LAV_CURRENT, version);
return (LAV_CURRENT);
}
if ((fd = open(MSG_ORIG(MSG_FMT_PROCSELF), O_RDONLY)) < 0)
return (LAV_CURRENT);
num = read(fd, &status, sizeof (status));
(void) close(fd);
if ((num != sizeof (status)) ||
prisinuse(&status.pr_sigtrace) || prisinuse(&status.pr_flttrace) ||
prisinuse(&status.pr_sysentry) || prisinuse(&status.pr_sysexit)) {
return (LAV_CURRENT);
}
if (dlinfo((void *)NULL, RTLD_DI_PROFILENAME, &pname) == -1)
(void) fprintf(stderr, MSG_INTL(MSG_GEN_PROFNOTSET));
return (LAV_CURRENT);
}
int
profile_open(const char *fname, Link_map *lmp)
{
size_t hsize;
size_t psize;
size_t csize;
size_t msize;
int i, fd, fixed = 0;
caddr_t lpc;
caddr_t hpc;
caddr_t addr;
struct stat status;
int new_buffer = 0;
sigset_t mask;
int err;
Ehdr * ehdr;
Phdr * phdr;
Dyn * dynp = 0;
Word nsym = 0;
if (*Profile == '\0') {
const char *dir, *suf;
char *tmp;
if (((tmp = strrchr(fname, '/')) != 0) && (*(++tmp)))
fname = tmp;
#if defined(_ELF64)
suf = MSG_ORIG(MSG_SUF_PROFILE_64);
#else
suf = MSG_ORIG(MSG_SUF_PROFILE);
#endif
if (dlinfo((void *)NULL, RTLD_DI_PROFILEOUT, &dir) == -1)
dir = MSG_ORIG(MSG_PTH_VARTMP);
(void) snprintf(Profile, MAXPATHLEN, MSG_ORIG(MSG_FMT_PROFILE),
dir, fname, suf);
}
if ((fd = open(Profile, (O_RDWR | O_CREAT), 0666)) == -1) {
err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), Profile,
strerror(err));
return (0);
}
lpc = hpc = (caddr_t)lmp->l_addr;
ehdr = (Ehdr *)lpc;
if (ehdr->e_phnum == 0) {
(void) close(fd);
return (0);
}
if (ehdr->e_type == ET_EXEC)
fixed = 1;
phdr = (Phdr *)(ehdr->e_phoff + lpc);
for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
caddr_t _hpc;
if (phdr->p_type == PT_DYNAMIC) {
dynp = (Dyn *)phdr->p_vaddr;
if (fixed == 0) {
dynp = (Dyn *)((unsigned long)dynp +
(unsigned long)lpc);
}
continue;
}
if (phdr->p_type != PT_LOAD)
continue;
_hpc = (caddr_t)(phdr->p_vaddr + phdr->p_memsz);
if (fixed == 0) {
_hpc = (caddr_t)((unsigned long)_hpc +
(unsigned long)lpc);
}
if (_hpc > hpc)
hpc = _hpc;
}
if (lpc == hpc) {
(void) close(fd);
return (0);
}
if (dynp) {
for (; dynp->d_tag != DT_NULL; dynp++) {
unsigned int *hashp;
if (dynp->d_tag != DT_HASH)
continue;
hashp = (unsigned int *)dynp->d_un.d_ptr;
if (fixed == 0) {
hashp = (unsigned int *)((unsigned long)hashp +
(unsigned long)lpc);
}
nsym = hashp[1];
break;
}
}
Lpc = lpc = (caddr_t)PRF_ROUNDWN((long)lpc, sizeof (long));
Hpc = hpc = (caddr_t)PRF_ROUNDUP((long)hpc, sizeof (long));
hsize = sizeof (L_hdr);
msize = (size_t)(hpc - lpc);
psize = (size_t)PRF_ROUNDUP((msize / PRF_BARSIZE), sizeof (long));
csize = (nsym + 1) * PRF_CGINIT * sizeof (L_cgarc);
Fsize = (hsize + psize + csize);
(void) fstat(fd, &status);
if (status.st_size == 0) {
if (ftruncate(fd, Fsize) == -1) {
err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_FTRUNC),
Profile, strerror(err));
(void) close(fd);
return (0);
}
new_buffer++;
} else
Fsize = status.st_size;
if ((addr = (caddr_t)mmap(0, Fsize, (PROT_READ | PROT_WRITE),
MAP_SHARED, fd, 0)) == (char *)-1) {
err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_MMAP), Profile,
strerror(err));
(void) close(fd);
return (0);
}
(void) close(fd);
Hptr = (L_hdr *)addr;
if (new_buffer)
(void) prof_mutex_init((lwp_mutex_t *)&Hptr->hd_mutex);
(void) prof_mutex_lock((mutex_t *)&Hptr->hd_mutex, &mask);
if (Hptr->hd_hpc) {
if (Hptr->hd_hpc != (caddr_t)(hpc - lpc)) {
(void) fprintf(stderr, MSG_INTL(MSG_GEN_PROFSZCHG),
Profile);
(void) prof_mutex_unlock((mutex_t *)&Hptr->
hd_mutex, &mask);
(void) munmap((caddr_t)Hptr, Fsize);
return (0);
}
} else {
Hptr->hd_magic = (unsigned int)PRF_MAGIC;
#if defined(_ELF64)
Hptr->hd_version = (unsigned int)PRF_VERSION_64;
#else
Hptr->hd_version = (unsigned int)PRF_VERSION;
#endif
Hptr->hd_hpc = (caddr_t)(hpc - lpc);
Hptr->hd_psize = (unsigned int)psize;
Hptr->hd_fsize = (unsigned int)Fsize;
Hptr->hd_ncndx = nsym;
Hptr->hd_lcndx = (nsym + 1) * PRF_CGINIT;
}
(void) prof_mutex_unlock((mutex_t *)&Hptr->hd_mutex, &mask);
Cptr = (L_cgarc *)(addr + hsize + psize);
profil((unsigned short *)(addr + hsize),
psize, (unsigned long)lpc, (unsigned int) PRF_SCALE);
return (1);
}
uint_t
la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
{
char *objname;
if (pname == 0)
return (0);
if (((objname = strrchr(lmp->l_name, '/')) == 0) ||
(*(++objname) == 0))
objname = lmp->l_name;
if ((strcmp(pname, objname) != 0) &&
(strcmp(pname, lmp->l_name) != 0))
return (LA_FLG_BINDFROM);
if (LIST(LINKMAP_TO_RTMAP(lmp))->lm_tflags & LML_TFLG_NOAUDIT)
return (LA_FLG_BINDFROM);
if (profile_open(pname, lmp) == 0)
return (0);
profcookie = *cookie;
return (LA_FLG_BINDFROM | LA_FLG_BINDTO);
}
uint_t
la_objclose(uintptr_t *cookie)
{
if (*cookie != profcookie)
return (0);
profcookie = 0;
profil(0, 0, 0, 0);
(void) munmap((caddr_t)Hptr, Fsize);
return (0);
}
static int
remap_profile(int fd)
{
caddr_t addr;
size_t l_fsize;
l_fsize = Hptr->hd_fsize;
if ((addr = (caddr_t)mmap(0, l_fsize, (PROT_READ | PROT_WRITE),
MAP_SHARED, fd, 0)) == (char *)-1) {
int err = errno;
(void) fprintf(stderr, MSG_INTL(MSG_SYS_MMAP), Profile,
strerror(err));
return (0);
}
(void) munmap((caddr_t)Hptr, Fsize);
Fsize = l_fsize;
Hptr = (L_hdr*) addr;
Cptr = (L_cgarc *)(addr + sizeof (L_hdr) + Hptr->hd_psize);
return (1);
}
uintptr_t
plt_cg_interp(uint_t ndx, caddr_t from, caddr_t to)
{
L_cgarc * cptr, cbucket;
sigset_t mask;
if (from != PRF_UNKNOWN) {
if ((from > Hpc) || (from < Lpc))
from = PRF_OUTADDR;
else
from = (caddr_t)(from - Lpc);
}
(void) prof_mutex_lock((mutex_t *)&Hptr->hd_mutex, &mask);
if (Hptr->hd_fsize != (unsigned int)Fsize) {
int fd;
fd = open(Profile, O_RDWR, 0);
if (remap_profile(fd) == 0) {
(void) prof_mutex_unlock((mutex_t *)&Hptr->hd_mutex,
&mask);
exit(1);
}
(void) close(fd);
}
cptr = &Cptr[ndx];
if (cptr->cg_to == 0) {
cptr->cg_to = (caddr_t)(to - Lpc);
cptr->cg_from = from;
if (from != PRF_UNKNOWN)
cptr->cg_count = 1;
} else {
if (from != PRF_UNKNOWN) {
while ((cptr->cg_from != from) &&
(cptr->cg_from != PRF_UNKNOWN)) {
if (cptr->cg_next != 0)
cptr = &Cptr[cptr->cg_next];
else {
to = cptr->cg_to;
cptr->cg_next = Hptr->hd_ncndx++;
cptr = &Cptr[cptr->cg_next];
if (Hptr->hd_ncndx == Hptr->hd_lcndx) {
caddr_t addr;
int fd;
Hptr->hd_fsize += (unsigned int)
PRF_CGNUMB *
sizeof (L_cgarc);
fd = open(Profile, O_RDWR, 0);
if (ftruncate(fd,
Hptr->hd_fsize) == -1) {
int err = errno;
(void) fprintf(stderr,
MSG_INTL(
MSG_SYS_FTRUNC),
Profile,
strerror(err));
(void) close(fd);
cptr = &cbucket;
}
addr = (caddr_t)((Addr)cptr -
(Addr)Cptr);
if (remap_profile(fd) == 0) {
(void) prof_mutex_unlock(
(mutex_t *)&Hptr->
hd_mutex, &mask);
exit(1);
}
cptr = (L_cgarc *)((Addr)addr +
(Addr)Cptr);
(void) close(fd);
Hptr->hd_lcndx += PRF_CGNUMB;
}
cptr->cg_from = from;
cptr->cg_to = to;
}
}
if (cptr->cg_from == PRF_UNKNOWN) {
cptr->cg_from = from;
cptr->cg_count = 1;
} else
cptr->cg_count++;
}
}
(void) prof_mutex_unlock((mutex_t *)&Hptr->hd_mutex, &mask);
return ((uintptr_t)((Addr)cptr->cg_to + (Addr)Lpc));
}
#if defined(__sparcv9)
uintptr_t
la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sbflags,
const char *sym_name)
#elif defined(__sparc)
uintptr_t
la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sbflags)
#elif defined(__amd64)
uintptr_t
la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sbflags,
const char *sym_name)
#elif defined(__i386)
uintptr_t
la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
uintptr_t *defcookie, La_i86_regs *regset, uint_t *sbflags)
#else
#error unexpected architecture!
#endif
{
caddr_t from;
if (profcookie == 0)
return (symp->st_value);
#if defined(__sparc)
from = (caddr_t)regset->lr_rego7;
#elif defined(__amd64)
from = *(caddr_t *)(regset->lr_rsp);
#elif defined(__i386)
from = *(caddr_t *)(regset->lr_esp);
#else
#error unexpected architecture!
#endif
return (plt_cg_interp(symndx, (caddr_t)from, (caddr_t)symp->st_value));
}