|
|
cde03b |
/****************************************************************/
|
|
|
cde03b |
/* mdso: midipix dso scavenger */
|
|
|
3e816c |
/* Copyright (C) 2015--2016 Z. Gilboa */
|
|
|
cde03b |
/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */
|
|
|
cde03b |
/****************************************************************/
|
|
|
cde03b |
|
|
|
cde03b |
#include <stdint.h>
|
|
|
cde03b |
#include <stddef.h>
|
|
|
cde03b |
#include <stdlib.h>
|
|
|
cde03b |
#include <string.h>
|
|
|
14073f |
#include <unistd.h>
|
|
|
14073f |
#include <errno.h>
|
|
|
cde03b |
#include <sys/mman.h>
|
|
|
cde03b |
|
|
|
cde03b |
#include <mdso/mdso.h>
|
|
|
cde03b |
#include "mdso_driver_impl.h"
|
|
|
4eae28 |
#include "mdso_errinfo_impl.h"
|
|
|
cde03b |
|
|
|
cde03b |
static int mdso_free_unit_ctx_impl(struct mdso_unit_ctx_impl * ctx, int status)
|
|
|
cde03b |
{
|
|
|
cde03b |
if (ctx) {
|
|
|
43f283 |
if (ctx->expsyms && ctx->expsyms->buffer)
|
|
|
43f283 |
free(ctx->expsyms->buffer);
|
|
|
43f283 |
|
|
|
43f283 |
if (ctx->expsyms)
|
|
|
43f283 |
free(ctx->expsyms);
|
|
|
43f283 |
|
|
|
cde03b |
mdso_unmap_input(&ctx->map);
|
|
|
cde03b |
free(ctx);
|
|
|
cde03b |
}
|
|
|
cde03b |
|
|
|
cde03b |
return status;
|
|
|
cde03b |
}
|
|
|
cde03b |
|
|
|
db4864 |
static FILE * mdso_stdin_to_tmp(const struct mdso_driver_ctx * dctx)
|
|
|
14073f |
{
|
|
|
db4864 |
struct mdso_driver_ctx_impl * ictx;
|
|
|
db4864 |
uintptr_t addr;
|
|
|
db4864 |
int fdtmp;
|
|
|
db4864 |
|
|
|
14073f |
FILE * ftmp;
|
|
|
14073f |
char buf[4096];
|
|
|
14073f |
ssize_t nread;
|
|
|
14073f |
int ret;
|
|
|
14073f |
|
|
|
db4864 |
addr = (uintptr_t)dctx - offsetof(struct mdso_driver_ctx_impl,ctx);
|
|
|
db4864 |
ictx = (struct mdso_driver_ctx_impl *)addr;
|
|
|
db4864 |
|
|
|
db4864 |
if (ictx->fdtmpin >= 0) {
|
|
|
db4864 |
if ((fdtmp = dup(ictx->fdtmpin)) < 0)
|
|
|
db4864 |
return 0;
|
|
|
db4864 |
|
|
|
919cb6 |
if (!(ftmp = fdopen(fdtmp,"r")))
|
|
|
919cb6 |
close(fdtmp);
|
|
|
919cb6 |
|
|
|
919cb6 |
return ftmp;
|
|
|
db4864 |
}
|
|
|
db4864 |
|
|
|
14073f |
if (!(ftmp = tmpfile()))
|
|
|
14073f |
return 0;
|
|
|
14073f |
|
|
|
db4864 |
if ((ictx->fdtmpin = dup(fileno(ftmp))) < 0) {
|
|
|
db4864 |
fclose(ftmp);
|
|
|
db4864 |
return 0;
|
|
|
db4864 |
}
|
|
|
db4864 |
|
|
|
14073f |
nread = read(0,buf,sizeof(buf)-1);
|
|
|
14073f |
|
|
|
14073f |
while (nread) {
|
|
|
14073f |
if (nread > 0) {
|
|
|
14073f |
buf[nread] = '\0';
|
|
|
14073f |
ret = fputs(buf,ftmp);
|
|
|
14073f |
} else
|
|
|
14073f |
ret = (errno == EINTR) ? 0 : -1;
|
|
|
14073f |
|
|
|
14073f |
if (ret < 0) {
|
|
|
14073f |
fclose(ftmp);
|
|
|
14073f |
return 0;
|
|
|
14073f |
}
|
|
|
14073f |
|
|
|
14073f |
nread = read(0,buf,sizeof(buf)-1);
|
|
|
14073f |
}
|
|
|
14073f |
|
|
|
14073f |
return ftmp;
|
|
|
14073f |
}
|
|
|
14073f |
|
|
|
43f283 |
static int mdso_create_symbol_vector(struct mdso_unit_ctx_impl * ctx)
|
|
|
43f283 |
{
|
|
|
43f283 |
int nsyms;
|
|
|
43f283 |
size_t nbytes;
|
|
|
43f283 |
size_t size;
|
|
|
43f283 |
char * dst;
|
|
|
43f283 |
const char * ch;
|
|
|
43f283 |
const char ** sym;
|
|
|
43f283 |
|
|
|
43f283 |
const char exphdr[] = "EXPORTS\n";
|
|
|
43f283 |
const char imphdr[] = "IMPORTS\n";
|
|
|
43f283 |
|
|
|
43f283 |
ch = ctx->map.addr;
|
|
|
43f283 |
nbytes = ctx->map.size;
|
|
|
43f283 |
|
|
|
43f283 |
for (nsyms=0; nbytes; ch++,nbytes--)
|
|
|
43f283 |
nsyms += (*ch == '\n');
|
|
|
43f283 |
|
|
|
43f283 |
size = offsetof(struct mdso_unit_ctx_impl,expsyms);
|
|
|
43f283 |
size += (nsyms+1)*sizeof(const char *);
|
|
|
43f283 |
|
|
|
bea9e7 |
if (!(ctx->expsyms = calloc(1,size)))
|
|
|
43f283 |
return -1;
|
|
|
43f283 |
|
|
|
bea9e7 |
if (!(ctx->expsyms->buffer = calloc(1,ctx->map.size)))
|
|
|
43f283 |
return -1;
|
|
|
43f283 |
|
|
|
43f283 |
ch = ctx->map.addr;
|
|
|
43f283 |
nbytes = ctx->map.size;
|
|
|
43f283 |
sym = ctx->expsyms->syms;
|
|
|
43f283 |
dst = ctx->expsyms->buffer;
|
|
|
43f283 |
size = strlen(exphdr);
|
|
|
43f283 |
|
|
|
43f283 |
/* support .def input files */
|
|
|
43f283 |
if ((nbytes >= size) && !(strncmp(ch,exphdr,size))) {
|
|
|
43f283 |
ch += size;
|
|
|
43f283 |
nbytes -= size;
|
|
|
43f283 |
}
|
|
|
43f283 |
|
|
|
43f283 |
/* advance to first symbol */
|
|
|
43f283 |
for (; nbytes && ((*ch==' ')
|
|
|
43f283 |
|| (*ch=='\t')
|
|
|
43f283 |
|| (*ch=='\r')
|
|
|
43f283 |
|| (*ch=='\n')); nbytes--)
|
|
|
43f283 |
ch++;
|
|
|
43f283 |
|
|
|
43f283 |
/* support .def input files */
|
|
|
43f283 |
size = strlen(imphdr);
|
|
|
43f283 |
|
|
|
43f283 |
while (nbytes && ((nbytes < size) || (strncmp(ch,imphdr,size)))) {
|
|
|
43f283 |
/* vector */
|
|
|
43f283 |
*sym++ = dst;
|
|
|
43f283 |
|
|
|
43f283 |
/* symbol */
|
|
|
43f283 |
for (; nbytes && ((*ch!=' ')
|
|
|
43f283 |
&& (*ch!='\t')
|
|
|
43f283 |
&& (*ch!='\r')
|
|
|
43f283 |
&& (*ch!='\n')); nbytes--)
|
|
|
43f283 |
*dst++ = *ch++;
|
|
|
43f283 |
|
|
|
43f283 |
dst++;
|
|
|
43f283 |
|
|
|
43f283 |
/* discard rest of input line */
|
|
|
43f283 |
for (; nbytes && (*ch!='\n'); nbytes--)
|
|
|
43f283 |
ch++;
|
|
|
43f283 |
|
|
|
43f283 |
/* advance to next symbol */
|
|
|
43f283 |
for (; nbytes && ((*ch==' ')
|
|
|
43f283 |
|| (*ch=='\t')
|
|
|
43f283 |
|| (*ch=='\r')
|
|
|
43f283 |
|| (*ch=='\n')); nbytes--)
|
|
|
43f283 |
ch++;
|
|
|
43f283 |
}
|
|
|
43f283 |
|
|
|
43f283 |
return 0;
|
|
|
43f283 |
}
|
|
|
43f283 |
|
|
|
cde03b |
int mdso_get_unit_ctx(
|
|
|
cde03b |
const struct mdso_driver_ctx * dctx,
|
|
|
cde03b |
const char * path,
|
|
|
cde03b |
struct mdso_unit_ctx ** pctx)
|
|
|
cde03b |
{
|
|
|
cde03b |
struct mdso_unit_ctx_impl * ctx;
|
|
|
14073f |
FILE * ftmp;
|
|
|
14073f |
int fd;
|
|
|
cde03b |
|
|
|
4eae28 |
if (!dctx)
|
|
|
4eae28 |
return MDSO_CUSTOM_ERROR(dctx,0);
|
|
|
4eae28 |
|
|
|
4eae28 |
else if (!(ctx = calloc(1,sizeof(*ctx))))
|
|
|
4eae28 |
return MDSO_BUFFER_ERROR(dctx);
|
|
|
cde03b |
|
|
|
14073f |
if (strcmp(path,"-"))
|
|
|
14073f |
fd = -1;
|
|
|
4eae28 |
|
|
|
db4864 |
else if (!(ftmp = mdso_stdin_to_tmp(dctx)))
|
|
|
4eae28 |
return mdso_free_unit_ctx_impl(
|
|
|
4eae28 |
ctx,MDSO_FILE_ERROR(dctx));
|
|
|
4eae28 |
|
|
|
14073f |
else if ((fd = dup(fileno(ftmp))) < 0)
|
|
|
4eae28 |
return mdso_free_unit_ctx_impl(
|
|
|
4eae28 |
ctx,MDSO_SYSTEM_ERROR(dctx));
|
|
|
4eae28 |
|
|
|
14073f |
else
|
|
|
14073f |
fclose(ftmp);
|
|
|
14073f |
|
|
|
51430e |
if (mdso_map_input(dctx,fd,path,PROT_READ,&ctx->map))
|
|
|
4eae28 |
return mdso_free_unit_ctx_impl(
|
|
|
51430e |
ctx,MDSO_NESTED_ERROR(dctx));
|
|
|
14073f |
|
|
|
14073f |
if (fd > 0)
|
|
|
14073f |
close(fd);
|
|
|
cde03b |
|
|
|
43f283 |
if (mdso_create_symbol_vector(ctx))
|
|
|
4eae28 |
return mdso_free_unit_ctx_impl(
|
|
|
4eae28 |
ctx,MDSO_BUFFER_ERROR(dctx));
|
|
|
43f283 |
|
|
|
cde03b |
memcpy(&ctx->cctx,dctx->cctx,
|
|
|
cde03b |
sizeof(ctx->cctx));
|
|
|
cde03b |
|
|
|
cde03b |
ctx->path = path;
|
|
|
cde03b |
|
|
|
cde03b |
ctx->uctx.path = &ctx->path;
|
|
|
cde03b |
ctx->uctx.map = &ctx->map;
|
|
|
cde03b |
ctx->uctx.cctx = &ctx->cctx;
|
|
|
43f283 |
ctx->uctx.syms = ctx->expsyms->syms;
|
|
|
cde03b |
|
|
|
cde03b |
*pctx = &ctx->uctx;
|
|
|
cde03b |
return 0;
|
|
|
cde03b |
}
|
|
|
cde03b |
|
|
|
cde03b |
void mdso_free_unit_ctx(struct mdso_unit_ctx * ctx)
|
|
|
cde03b |
{
|
|
|
cde03b |
struct mdso_unit_ctx_impl * ictx;
|
|
|
cde03b |
uintptr_t addr;
|
|
|
cde03b |
|
|
|
cde03b |
if (ctx) {
|
|
|
cde03b |
addr = (uintptr_t)ctx - offsetof(struct mdso_unit_ctx_impl,uctx);
|
|
|
cde03b |
ictx = (struct mdso_unit_ctx_impl *)addr;
|
|
|
cde03b |
mdso_free_unit_ctx_impl(ictx,0);
|
|
|
cde03b |
}
|
|
|
cde03b |
}
|