}
void
-wputl(ushort w)
+wputl(uint16 w)
{
cput(w);
cput(w>>8);
}
void
-wput(ushort w)
+wputb(uint16 w)
{
cput(w>>8);
cput(w);
}
void
-lput(int32 l)
+lputb(int32 l)
{
cput(l>>24);
cput(l>>16);
}
void
-vput(vlong v)
+vputb(uint64 v)
{
- lput(v>>32);
- lput(v);
+ lputb(v>>32);
+ lputb(v);
}
void
}
void
-vputl(vlong v)
+vputl(uint64 v)
{
lputl(v);
lputl(v>>32);
uchar *op1;
vlong vl, va, fo, w, symo;
vlong symdatva = 0x99LL<<32;
- int strtabindex;
+ Elf64Hdr *eh;
Elf64PHdr *ph;
Elf64SHdr *sh;
- char eident[EI_NIDENT];
if(debug['v'])
Bprint(&bso, "%5.2f asmb\n", cputime());
magic |= 0x00008000; /* fat header */
if(dlm)
magic |= 0x80000000; /* dlm */
- lput(magic); /* magic */
- lput(textsize); /* sizes */
- lput(datsize);
- lput(bsssize);
- lput(symsize); /* nsyms */
+ lputb(magic); /* magic */
+ lputb(textsize); /* sizes */
+ lputb(datsize);
+ lputb(bsssize);
+ lputb(symsize); /* nsyms */
vl = entryvalue();
- lput(PADDR(vl)); /* va of entry */
- lput(spsize); /* sp offsets */
- lput(lcsize); /* line offsets */
- vput(vl); /* va of entry */
+ lputb(PADDR(vl)); /* va of entry */
+ lputb(spsize); /* sp offsets */
+ lputb(lcsize); /* line offsets */
+ vputb(vl); /* va of entry */
break;
case 3: /* plan9 */
magic = 4*26*26+7;
if(dlm)
magic |= 0x80000000;
- lput(magic); /* magic */
- lput(textsize); /* sizes */
- lput(datsize);
- lput(bsssize);
- lput(symsize); /* nsyms */
- lput(entryvalue()); /* va of entry */
- lput(spsize); /* sp offsets */
- lput(lcsize); /* line offsets */
+ lputb(magic); /* magic */
+ lputb(textsize); /* sizes */
+ lputb(datsize);
+ lputb(bsssize);
+ lputb(symsize); /* nsyms */
+ lputb(entryvalue()); /* va of entry */
+ lputb(spsize); /* sp offsets */
+ lputb(lcsize); /* line offsets */
break;
case 5:
strnput("\177ELF", 4); /* e_ident */
w = STRTABSIZE;
- strtabindex = nume64shdr;
sh = newElf64SHdr(".shstrtab");
sh->type = SHT_STRTAB;
sh->off = fo;
sh->addralign = 1;
sh->entsize = 24;
- // write out the main header */
- memset(eident, 0, sizeof eident);
- eident[EI_MAG0] = '\177';
- eident[EI_MAG1] = 'E';
- eident[EI_MAG2] = 'L';
- eident[EI_MAG3] = 'F';
- eident[EI_CLASS] = ELFCLASS64;
- eident[EI_DATA] = ELFDATA2LSB;
- eident[EI_VERSION] = EV_CURRENT;
- strnput(eident, EI_NIDENT);
-
- wputl(ET_EXEC); /* type = EXEC */
- wputl(62); /* machine = AMD64 */
- lputl(EV_CURRENT); /* version = CURRENT */
- vputl(entryvalue()); /* entry vaddr */
- vputl(64L); /* offset to first phdr */
- vputl(64L+56*nume64phdr); /* offset to first shdr */
- lputl(0L); /* processor specific flags */
- wputl(64); /* Ehdr size */
- wputl(56); /* Phdr size */
- wputl(nume64phdr); /* # of Phdrs */
- wputl(64); /* Shdr size */
- wputl(nume64shdr); /* # of Shdrs */
- wputl(strtabindex); /* Shdr with strings */
-
+ // main header */
+ eh = getElf64Hdr();
+ eh->ident[EI_MAG0] = '\177';
+ eh->ident[EI_MAG1] = 'E';
+ eh->ident[EI_MAG2] = 'L';
+ eh->ident[EI_MAG3] = 'F';
+ eh->ident[EI_CLASS] = ELFCLASS64;
+ eh->ident[EI_DATA] = ELFDATA2LSB;
+ eh->ident[EI_VERSION] = EV_CURRENT;
+
+ eh->type = ET_EXEC;
+ 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();
cflush();
#include "../ld/elf64.h"
#define NSECT 16
-int nume64phdr;
-int nume64shdr;
-int nume64str;
+static int nume64str;
+static Elf64Hdr hdr;
static Elf64PHdr *phdr[NSECT];
static Elf64SHdr *shdr[NSECT];
static char *sname[NSECT];
void
elf64phdr(Elf64PHdr *e)
{
- lputl(e->type);
- lputl(e->flags);
- vputl(e->off);
- vputl(e->vaddr);
- vputl(e->paddr);
- vputl(e->filesz);
- vputl(e->memsz);
- vputl(e->align);
+ LPUT(e->type);
+ LPUT(e->flags);
+ VPUT(e->off);
+ VPUT(e->vaddr);
+ VPUT(e->paddr);
+ VPUT(e->filesz);
+ VPUT(e->memsz);
+ VPUT(e->align);
}
void
elf64shdr(char *name, Elf64SHdr *e)
{
- lputl(e->name);
- lputl(e->type);
- vputl(e->flags);
- vputl(e->addr);
- vputl(e->off);
- vputl(e->size);
- lputl(e->link);
- lputl(e->info);
- vputl(e->addralign);
- vputl(e->entsize);
+ LPUT(e->name);
+ LPUT(e->type);
+ VPUT(e->flags);
+ VPUT(e->addr);
+ VPUT(e->off);
+ VPUT(e->size);
+ LPUT(e->link);
+ LPUT(e->info);
+ VPUT(e->addralign);
+ VPUT(e->entsize);
}
int
{
int i;
- for (i = 0; i < nume64shdr; i++)
+ for (i = 0; i < hdr.shnum; i++)
elf64shdr(sname[i], shdr[i]);
}
{
int i;
- for (i = 0; i < nume64phdr; i++)
+ for (i = 0; i < hdr.phnum; i++)
elf64phdr(phdr[i]);
}
e = malloc(sizeof *e);
memset(e, 0, sizeof *e);
- if (nume64phdr >= NSECT)
+ if (hdr.phnum >= NSECT)
diag("too many phdrs");
else
- phdr[nume64phdr++] = e;
+ phdr[hdr.phnum++] = e;
return e;
}
{
Elf64SHdr *e;
+ if (strcmp(name, ".shstrtab") == 0)
+ hdr.shstrndx = hdr.shnum;
e = malloc(sizeof *e);
memset(e, 0, sizeof *e);
e->name = stroffset;
- if (nume64shdr >= NSECT) {
+ if (hdr.shnum >= NSECT) {
diag("too many shdrs");
} else {
e64addstr(name);
- shdr[nume64shdr++] = e;
+ shdr[hdr.shnum++] = e;
}
return e;
}
+
+Elf64Hdr*
+getElf64Hdr(void)
+{
+ return &hdr;
+}
+
+void
+elf64writehdr()
+{
+ int i;
+
+ for (i = 0; i < EI_NIDENT; i++)
+ cput(hdr.ident[i]);
+ WPUT(hdr.type);
+ WPUT(hdr.machine);
+ LPUT(hdr.version);
+ VPUT(hdr.entry);
+ VPUT(hdr.phoff);
+ VPUT(hdr.shoff);
+ LPUT(hdr.flags);
+ WPUT(hdr.ehsize);
+ WPUT(hdr.phentsize);
+ WPUT(hdr.phnum);
+ WPUT(hdr.shentsize);
+ WPUT(hdr.shnum);
+ WPUT(hdr.shstrndx);
+}