2005-04-21 16:53:53 +02:00
|
|
|
/* $Header$ */
|
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
2010-07-05 01:01:32 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "out.h"
|
2005-04-21 16:53:53 +02:00
|
|
|
#include "object.h"
|
2010-07-05 01:01:32 +02:00
|
|
|
#include "rd.h"
|
|
|
|
#include "rd_bytes.h"
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
extern long lseek();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parts of the output file.
|
|
|
|
*/
|
|
|
|
#undef PARTEMIT
|
|
|
|
#undef PARTRELO
|
|
|
|
#undef PARTNAME
|
|
|
|
#undef PARTCHAR
|
|
|
|
#undef PARTDBUG
|
|
|
|
#undef NPARTS
|
|
|
|
|
|
|
|
#define PARTEMIT 0
|
|
|
|
#define PARTRELO 1
|
|
|
|
#define PARTNAME 2
|
|
|
|
#define PARTCHAR 3
|
|
|
|
#ifdef SYMDBUG
|
|
|
|
#define PARTDBUG 4
|
|
|
|
#else
|
|
|
|
#define PARTDBUG 3
|
|
|
|
#endif
|
|
|
|
#define NPARTS (PARTDBUG + 1)
|
|
|
|
|
|
|
|
static long offset[MAXSECT];
|
|
|
|
|
|
|
|
static int outfile;
|
|
|
|
static long outseek[NPARTS];
|
|
|
|
static long currpos;
|
|
|
|
static long rd_base;
|
|
|
|
#define OUTSECT(i) \
|
|
|
|
(outseek[PARTEMIT] = offset[i])
|
|
|
|
#define BEGINSEEK(p, o) \
|
|
|
|
(outseek[(p)] = (o))
|
|
|
|
|
|
|
|
static int sectionnr;
|
|
|
|
|
|
|
|
static
|
2010-07-05 01:01:32 +02:00
|
|
|
void OUTREAD(int p, char *b, long n)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
register long l = outseek[p];
|
|
|
|
|
|
|
|
if (currpos != l) {
|
|
|
|
lseek(outfile, l, 0);
|
|
|
|
}
|
|
|
|
rd_bytes(outfile, b, n);
|
|
|
|
l += n;
|
|
|
|
currpos = l;
|
|
|
|
outseek[p] = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the output file according to the chosen strategy.
|
|
|
|
*/
|
|
|
|
int
|
2010-07-05 01:01:32 +02:00
|
|
|
rd_open(char *f)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2010-07-05 01:01:32 +02:00
|
|
|
int outfile = open(f, 0);
|
|
|
|
if (outfile < 0)
|
2005-04-21 16:53:53 +02:00
|
|
|
return 0;
|
|
|
|
return rd_fdopen(outfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int offcnt;
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
int rd_fdopen(int fd)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NPARTS; i++) outseek[i] = 0;
|
|
|
|
offcnt = 0;
|
|
|
|
rd_base = lseek(fd, 0L, 1);
|
|
|
|
if (rd_base < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
currpos = rd_base;
|
|
|
|
outseek[PARTEMIT] = currpos;
|
|
|
|
outfile = fd;
|
|
|
|
sectionnr = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_close()
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
close(outfile);
|
|
|
|
outfile = -1;
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
int rd_fd()
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
return outfile;
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_ohead(register struct outhead *head)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
register long off;
|
|
|
|
|
|
|
|
OUTREAD(PARTEMIT, (char *) head, (long) SZ_HEAD);
|
|
|
|
#if ! (BYTES_REVERSED || WORDS_REVERSED)
|
|
|
|
if (sizeof(struct outhead) != SZ_HEAD)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
register char *c = (char *) head + (SZ_HEAD-4);
|
|
|
|
|
|
|
|
head->oh_nchar = get4(c);
|
|
|
|
c -= 4; head->oh_nemit = get4(c);
|
|
|
|
c -= 2; head->oh_nname = uget2(c);
|
|
|
|
c -= 2; head->oh_nrelo = uget2(c);
|
|
|
|
c -= 2; head->oh_nsect = uget2(c);
|
|
|
|
c -= 2; head->oh_flags = uget2(c);
|
|
|
|
c -= 2; head->oh_stamp = uget2(c);
|
|
|
|
c -= 2; head->oh_magic = uget2(c);
|
|
|
|
}
|
|
|
|
off = OFF_RELO(*head) + rd_base;
|
|
|
|
BEGINSEEK(PARTRELO, off);
|
|
|
|
off += (long) head->oh_nrelo * SZ_RELO;
|
|
|
|
BEGINSEEK(PARTNAME, off);
|
|
|
|
off += (long) head->oh_nname * SZ_NAME;
|
|
|
|
BEGINSEEK(PARTCHAR, off);
|
|
|
|
#ifdef SYMDBUG
|
|
|
|
off += head->oh_nchar;
|
|
|
|
BEGINSEEK(PARTDBUG, off);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_rew_relos(head)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct outhead *head;
|
|
|
|
{
|
|
|
|
register long off = OFF_RELO(*head) + rd_base;
|
|
|
|
|
|
|
|
BEGINSEEK(PARTRELO, off);
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_sect(sect, cnt)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct outsect *sect;
|
|
|
|
register unsigned int cnt;
|
|
|
|
{
|
|
|
|
register char *c = (char *) sect + cnt * SZ_SECT;
|
|
|
|
|
|
|
|
OUTREAD(PARTEMIT, (char *) sect, (long)cnt * SZ_SECT);
|
|
|
|
sect += cnt;
|
|
|
|
offcnt += cnt;
|
|
|
|
while (cnt--) {
|
|
|
|
sect--;
|
|
|
|
#if ! (BYTES_REVERSED || WORDS_REVERSED)
|
|
|
|
if (sizeof(struct outsect) != SZ_SECT)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
c -= 4; sect->os_lign = get4(c);
|
|
|
|
c -= 4; sect->os_flen = get4(c);
|
|
|
|
c -= 4; sect->os_foff = get4(c);
|
|
|
|
}
|
|
|
|
offset[--offcnt] = sect->os_foff + rd_base;
|
|
|
|
#if ! (BYTES_REVERSED || WORDS_REVERSED)
|
|
|
|
if (sizeof(struct outsect) != SZ_SECT)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
c -= 4; sect->os_size = get4(c);
|
|
|
|
c -= 4; sect->os_base = get4(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_outsect(int s)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
OUTSECT(s);
|
|
|
|
sectionnr = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't have to worry about byte order here.
|
|
|
|
*/
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_emit(emit, cnt)
|
2005-04-21 16:53:53 +02:00
|
|
|
char *emit;
|
|
|
|
long cnt;
|
|
|
|
{
|
|
|
|
OUTREAD(PARTEMIT, emit, cnt);
|
|
|
|
offset[sectionnr] += cnt;
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_relo(relo, cnt)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct outrelo *relo;
|
|
|
|
register unsigned int cnt;
|
|
|
|
{
|
|
|
|
OUTREAD(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
|
|
|
|
#if ! (BYTES_REVERSED || WORDS_REVERSED)
|
|
|
|
if (sizeof(struct outrelo) != SZ_RELO)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
register char *c = (char *) relo + (long) cnt * SZ_RELO;
|
|
|
|
|
|
|
|
relo += cnt;
|
|
|
|
while (cnt--) {
|
|
|
|
relo--;
|
|
|
|
c -= 4; relo->or_addr = get4(c);
|
|
|
|
c -= 2; relo->or_nami = uget2(c);
|
|
|
|
relo->or_sect = *--c;
|
|
|
|
relo->or_type = *--c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_name(struct outname *name, unsigned int cnt)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
OUTREAD(PARTNAME, (char *) name, (long) cnt * SZ_NAME);
|
|
|
|
#if ! (BYTES_REVERSED || WORDS_REVERSED)
|
|
|
|
if (sizeof(struct outname) != SZ_NAME)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
register char *c = (char *) name + (long) cnt * SZ_NAME;
|
|
|
|
|
|
|
|
name += cnt;
|
|
|
|
while (cnt--) {
|
|
|
|
name--;
|
|
|
|
c -= 4; name->on_valu = get4(c);
|
|
|
|
c -= 2; name->on_desc = uget2(c);
|
|
|
|
c -= 2; name->on_type = uget2(c);
|
|
|
|
c -= 4; name->on_foff = get4(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_string(char *addr, long len)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
OUTREAD(PARTCHAR, addr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SYMDBUG
|
2010-07-05 01:01:32 +02:00
|
|
|
void rd_dbug(char *buf, long size)
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
OUTREAD(PARTDBUG, buf, size);
|
|
|
|
}
|
|
|
|
#endif
|