debug['8'] = 1; /* 64-bit addresses */
v = rnd(HEADR+textsize, INITRND);
seek(cout, v, 0);
+ elf64init();
break;
}
eh->machine = 62; /* machine = AMD64 */
eh->version = EV_CURRENT;
eh->entry = entryvalue();
- eh->phoff = 64L;
- eh->shoff = 64L+56*eh->phnum;
- eh->ehsize = 64;
- eh->phentsize = 56;
- eh->shentsize = 64;
-
- elf64writehdr();
- elf64writephdrs();
- elf64writeshdrs();
+
+ a = 0;
+ a += elf64writehdr();
+ a += elf64writephdrs();
+ a += elf64writeshdrs();
+ if (a > ELF64FULLHDRSIZE) {
+ diag("ELF64FULLHDRSIZE too small:", a);
+ }
cflush();
/* string table */
INITRND = 4096;
break;
case 7: /* elf64 executable */
- HEADR = elf64headr();
+ HEADR = ELF64FULLHDRSIZE;
if(INITTEXT == -1)
INITTEXT = (1<<22)+HEADR;
if(INITDAT == -1)
#include "../ld/elf64.h"
#define NSECT 16
-static int nume64str;
+static int numstr;
static Elf64Hdr hdr;
static Elf64PHdr *phdr[NSECT];
static Elf64SHdr *shdr[NSECT];
static char *sname[NSECT];
static char *str[NSECT];
+void
+elf64init(void)
+{
+ hdr.phoff = ELF64HDRSIZE;
+ hdr.shoff = ELF64HDRSIZE;
+ hdr.ehsize = ELF64HDRSIZE;
+ hdr.phentsize = ELF64PHDRSIZE;
+ hdr.shentsize = ELF64SHDRSIZE;
+}
+
void
elf64phdr(Elf64PHdr *e)
{
elf64writestrtable(void)
{
int i;
- int size;
+ uint32 size;
size = 0;
- for (i = 0; i < nume64str; i++)
+ for (i = 0; i < numstr; i++)
size += putelf64strtab(str[i]);
if (size > STRTABSIZE)
diag("elf64 string table overflow");
void
e64addstr(char *name)
{
- if (nume64str >= NSECT) {
+ if (numstr >= NSECT) {
diag("too many elf strings");
return;
}
- str[nume64str++] = strdup(name);
+ str[numstr++] = strdup(name);
stroffset += strlen(name)+1;
}
uint32
-elf64headr(void)
-{
- uint32 a;
-
- a = 64; /* a.out header */
-
- /* TODO: calculate these byte counts properly */
- a += 56; /* page zero seg */
- a += 56; /* text seg */
- a += 56; /* stack seg */
-
- a += 64; /* nil sect */
- a += 64; /* .text sect */
- a += 64; /* .data seg */
- a += 64; /* .bss sect */
- a += 64; /* .shstrtab sect - strings for headers */
- if (!debug['s']) {
- a += 56; /* symdat seg */
- a += 64; /* .gosymtab sect */
- a += 64; /* .gopclntab sect */
- }
-
- return a;
-}
-
-void
elf64writeshdrs(void)
{
int i;
for (i = 0; i < hdr.shnum; i++)
elf64shdr(sname[i], shdr[i]);
+ return hdr.shnum * ELF64SHDRSIZE;
}
-void
+uint32
elf64writephdrs(void)
{
int i;
for (i = 0; i < hdr.phnum; i++)
elf64phdr(phdr[i]);
+ return hdr.phnum * ELF64PHDRSIZE;
}
Elf64PHdr*
diag("too many phdrs");
else
phdr[hdr.phnum++] = e;
+ hdr.shoff += ELF64PHDRSIZE;
return e;
}
return &hdr;
}
-void
+uint32
elf64writehdr()
{
int i;
WPUT(hdr.shentsize);
WPUT(hdr.shnum);
WPUT(hdr.shstrndx);
+ return ELF64HDRSIZE;
}
Elf64_Half shnum; /* Number of section header entries */
Elf64_Half shstrndx; /* Section name string table index */
};
+#define ELF64HDRSIZE 64
/* E ident indexes */
#define EI_MAG0 0 /* File identification */
Elf64_Xword memsz; /* Size of segment in memory */
Elf64_Xword align; /* Alignment of segment */
};
+#define ELF64PHDRSIZE 56
/* P types */
#define PT_NULL 0 /* Unused entry */
Elf64_Xword addralign; /* Address alignment boundary */
Elf64_Xword entsize; /* Size of entries, if section has table */
};
+#define ELF64SHDRSIZE 64
/* S types */
#define SHT_NULL 0 /* Unused section header */
#define SHF_MASKOS 0x0F000000 /* Environment-specific use */
#define SHF_MASKPROC 0xF0000000 /* Processor-specific use */
+void elf64init(void);
Elf64Hdr *getElf64Hdr();
Elf64SHdr *newElf64SHdr(char*);
Elf64PHdr *newElf64PHdr();
-uint32 elf64headr(void);
-void elf64writehdr(void);
-void elf64writephdrs(void);
-void elf64writeshdrs(void);
+uint32 elf64writehdr(void);
+uint32 elf64writephdrs(void);
+uint32 elf64writeshdrs(void);
void elf64writestrtable(void);
extern int nume64phdr;
extern int nume64shdr;
#define STRTABSIZE 256
+/* Amount of space to reserve at the start of the file; may waste some */
+#define ELF64FULLHDRSIZE 2048