Commit fe734d86 authored by Rob Pike's avatar Rob Pike

clean up generation of exec header.

make endianness explicit when writing values.

R=rsc
DELTA=129  (37 added, 7 deleted, 85 changed)
OCL=31826
CL=31854
parent 94d89ede
...@@ -60,21 +60,21 @@ entryvalue(void) ...@@ -60,21 +60,21 @@ entryvalue(void)
} }
void void
wputl(ushort w) wputl(uint16 w)
{ {
cput(w); cput(w);
cput(w>>8); cput(w>>8);
} }
void void
wput(ushort w) wputb(uint16 w)
{ {
cput(w>>8); cput(w>>8);
cput(w); cput(w);
} }
void void
lput(int32 l) lputb(int32 l)
{ {
cput(l>>24); cput(l>>24);
cput(l>>16); cput(l>>16);
...@@ -83,10 +83,10 @@ lput(int32 l) ...@@ -83,10 +83,10 @@ lput(int32 l)
} }
void void
vput(vlong v) vputb(uint64 v)
{ {
lput(v>>32); lputb(v>>32);
lput(v); lputb(v);
} }
void void
...@@ -99,7 +99,7 @@ lputl(int32 l) ...@@ -99,7 +99,7 @@ lputl(int32 l)
} }
void void
vputl(vlong v) vputl(uint64 v)
{ {
lputl(v); lputl(v);
lputl(v>>32); lputl(v>>32);
...@@ -126,10 +126,9 @@ asmb(void) ...@@ -126,10 +126,9 @@ asmb(void)
uchar *op1; uchar *op1;
vlong vl, va, fo, w, symo; vlong vl, va, fo, w, symo;
vlong symdatva = 0x99LL<<32; vlong symdatva = 0x99LL<<32;
int strtabindex; Elf64Hdr *eh;
Elf64PHdr *ph; Elf64PHdr *ph;
Elf64SHdr *sh; Elf64SHdr *sh;
char eident[EI_NIDENT];
if(debug['v']) if(debug['v'])
Bprint(&bso, "%5.2f asmb\n", cputime()); Bprint(&bso, "%5.2f asmb\n", cputime());
...@@ -277,29 +276,29 @@ asmb(void) ...@@ -277,29 +276,29 @@ asmb(void)
magic |= 0x00008000; /* fat header */ magic |= 0x00008000; /* fat header */
if(dlm) if(dlm)
magic |= 0x80000000; /* dlm */ magic |= 0x80000000; /* dlm */
lput(magic); /* magic */ lputb(magic); /* magic */
lput(textsize); /* sizes */ lputb(textsize); /* sizes */
lput(datsize); lputb(datsize);
lput(bsssize); lputb(bsssize);
lput(symsize); /* nsyms */ lputb(symsize); /* nsyms */
vl = entryvalue(); vl = entryvalue();
lput(PADDR(vl)); /* va of entry */ lputb(PADDR(vl)); /* va of entry */
lput(spsize); /* sp offsets */ lputb(spsize); /* sp offsets */
lput(lcsize); /* line offsets */ lputb(lcsize); /* line offsets */
vput(vl); /* va of entry */ vputb(vl); /* va of entry */
break; break;
case 3: /* plan9 */ case 3: /* plan9 */
magic = 4*26*26+7; magic = 4*26*26+7;
if(dlm) if(dlm)
magic |= 0x80000000; magic |= 0x80000000;
lput(magic); /* magic */ lputb(magic); /* magic */
lput(textsize); /* sizes */ lputb(textsize); /* sizes */
lput(datsize); lputb(datsize);
lput(bsssize); lputb(bsssize);
lput(symsize); /* nsyms */ lputb(symsize); /* nsyms */
lput(entryvalue()); /* va of entry */ lputb(entryvalue()); /* va of entry */
lput(spsize); /* sp offsets */ lputb(spsize); /* sp offsets */
lput(lcsize); /* line offsets */ lputb(lcsize); /* line offsets */
break; break;
case 5: case 5:
strnput("\177ELF", 4); /* e_ident */ strnput("\177ELF", 4); /* e_ident */
...@@ -503,7 +502,6 @@ asmb(void) ...@@ -503,7 +502,6 @@ asmb(void)
w = STRTABSIZE; w = STRTABSIZE;
strtabindex = nume64shdr;
sh = newElf64SHdr(".shstrtab"); sh = newElf64SHdr(".shstrtab");
sh->type = SHT_STRTAB; sh->type = SHT_STRTAB;
sh->off = fo; sh->off = fo;
...@@ -533,31 +531,27 @@ asmb(void) ...@@ -533,31 +531,27 @@ asmb(void)
sh->addralign = 1; sh->addralign = 1;
sh->entsize = 24; sh->entsize = 24;
// write out the main header */ // main header */
memset(eident, 0, sizeof eident); eh = getElf64Hdr();
eident[EI_MAG0] = '\177'; eh->ident[EI_MAG0] = '\177';
eident[EI_MAG1] = 'E'; eh->ident[EI_MAG1] = 'E';
eident[EI_MAG2] = 'L'; eh->ident[EI_MAG2] = 'L';
eident[EI_MAG3] = 'F'; eh->ident[EI_MAG3] = 'F';
eident[EI_CLASS] = ELFCLASS64; eh->ident[EI_CLASS] = ELFCLASS64;
eident[EI_DATA] = ELFDATA2LSB; eh->ident[EI_DATA] = ELFDATA2LSB;
eident[EI_VERSION] = EV_CURRENT; eh->ident[EI_VERSION] = EV_CURRENT;
strnput(eident, EI_NIDENT);
eh->type = ET_EXEC;
wputl(ET_EXEC); /* type = EXEC */ eh->machine = 62; /* machine = AMD64 */
wputl(62); /* machine = AMD64 */ eh->version = EV_CURRENT;
lputl(EV_CURRENT); /* version = CURRENT */ eh->entry = entryvalue();
vputl(entryvalue()); /* entry vaddr */ eh->phoff = 64L;
vputl(64L); /* offset to first phdr */ eh->shoff = 64L+56*eh->phnum;
vputl(64L+56*nume64phdr); /* offset to first shdr */ eh->ehsize = 64;
lputl(0L); /* processor specific flags */ eh->phentsize = 56;
wputl(64); /* Ehdr size */ eh->shentsize = 64;
wputl(56); /* Phdr size */
wputl(nume64phdr); /* # of Phdrs */ elf64writehdr();
wputl(64); /* Shdr size */
wputl(nume64shdr); /* # of Shdrs */
wputl(strtabindex); /* Shdr with strings */
elf64writephdrs(); elf64writephdrs();
elf64writeshdrs(); elf64writeshdrs();
cflush(); cflush();
......
...@@ -413,7 +413,7 @@ void ldpkg(Biobuf*, int64, char*); ...@@ -413,7 +413,7 @@ void ldpkg(Biobuf*, int64, char*);
void loadlib(void); void loadlib(void);
void listinit(void); void listinit(void);
Sym* lookup(char*, int); Sym* lookup(char*, int);
void lput(int32); void lputb(int32);
void lputl(int32); void lputl(int32);
void main(int, char*[]); void main(int, char*[]);
void mkfwd(void); void mkfwd(void);
...@@ -436,8 +436,9 @@ void strnput(char*, int); ...@@ -436,8 +436,9 @@ void strnput(char*, int);
void undef(void); void undef(void);
void undefsym(Sym*); void undefsym(Sym*);
vlong vaddr(Adr*); vlong vaddr(Adr*);
void vputl(vlong); void vputl(uint64);
void wput(ushort); void wputb(uint16);
void wputl(uint16);
void xdefine(char*, int, vlong); void xdefine(char*, int, vlong);
void xfol(Prog*); void xfol(Prog*);
void zaddr(Biobuf*, Adr*, Sym*[]); void zaddr(Biobuf*, Adr*, Sym*[]);
...@@ -450,6 +451,10 @@ void machstack(vlong); ...@@ -450,6 +451,10 @@ void machstack(vlong);
void machdylink(void); void machdylink(void);
uint32 machheadr(void); uint32 machheadr(void);
/* Native is little-endian */
#define LPUT(a) lputl(a)
#define WPUT(a) wputl(a)
#define VPUT(a) vputl(a)
#pragma varargck type "D" Adr* #pragma varargck type "D" Adr*
#pragma varargck type "P" Prog* #pragma varargck type "P" Prog*
......
...@@ -158,10 +158,10 @@ putsymb(char *s, int t, vlong v, int ver, char *go) ...@@ -158,10 +158,10 @@ putsymb(char *s, int t, vlong v, int ver, char *go)
s++; s++;
l = 4; l = 4;
if(!debug['8']){ if(!debug['8']){
lput(v>>32); lputb(v>>32);
l = 8; l = 8;
} }
lput(v); lputb(v);
if(ver) if(ver)
t += 'a' - 'A'; t += 'a' - 'A';
cput(t+0x80); /* 0x80 is variable length */ cput(t+0x80); /* 0x80 is variable length */
...@@ -1726,14 +1726,14 @@ asmdyn() ...@@ -1726,14 +1726,14 @@ asmdyn()
cflush(); cflush();
off = seek(cout, 0, 1); off = seek(cout, 0, 1);
lput(0); lputb(0);
t = 0; t = 0;
lput(imports); lputb(imports);
t += 4; t += 4;
for(i = 0; i < NHASH; i++) for(i = 0; i < NHASH; i++)
for(s = hash[i]; s != S; s = s->link) for(s = hash[i]; s != S; s = s->link)
if(s->type == SUNDEF){ if(s->type == SUNDEF){
lput(s->sig); lputb(s->sig);
t += 4; t += 4;
t += sput(s->name); t += sput(s->name);
} }
...@@ -1743,7 +1743,7 @@ asmdyn() ...@@ -1743,7 +1743,7 @@ asmdyn()
n = r->n; n = r->n;
m = r->m; m = r->m;
a = r->a; a = r->a;
lput(n); lputb(n);
t += 4; t += 4;
for(i = 0; i < n; i++){ for(i = 0; i < n; i++){
ra = *a-la; ra = *a-la;
...@@ -1762,11 +1762,11 @@ asmdyn() ...@@ -1762,11 +1762,11 @@ asmdyn()
t++; t++;
} }
else if(c == 1){ else if(c == 1){
wput(ra); wputb(ra);
t += 2; t += 2;
} }
else{ else{
lput(ra); lputb(ra);
t += 4; t += 4;
} }
la = *a++; la = *a++;
...@@ -1774,7 +1774,7 @@ asmdyn() ...@@ -1774,7 +1774,7 @@ asmdyn()
cflush(); cflush();
seek(cout, off, 0); seek(cout, off, 0);
lput(t); lputb(t);
if(debug['v']){ if(debug['v']){
Bprint(&bso, "import table entries = %d\n", imports); Bprint(&bso, "import table entries = %d\n", imports);
......
...@@ -7,9 +7,8 @@ ...@@ -7,9 +7,8 @@
#include "../ld/elf64.h" #include "../ld/elf64.h"
#define NSECT 16 #define NSECT 16
int nume64phdr; static int nume64str;
int nume64shdr; static Elf64Hdr hdr;
int nume64str;
static Elf64PHdr *phdr[NSECT]; static Elf64PHdr *phdr[NSECT];
static Elf64SHdr *shdr[NSECT]; static Elf64SHdr *shdr[NSECT];
static char *sname[NSECT]; static char *sname[NSECT];
...@@ -18,29 +17,29 @@ static char *str[NSECT]; ...@@ -18,29 +17,29 @@ static char *str[NSECT];
void void
elf64phdr(Elf64PHdr *e) elf64phdr(Elf64PHdr *e)
{ {
lputl(e->type); LPUT(e->type);
lputl(e->flags); LPUT(e->flags);
vputl(e->off); VPUT(e->off);
vputl(e->vaddr); VPUT(e->vaddr);
vputl(e->paddr); VPUT(e->paddr);
vputl(e->filesz); VPUT(e->filesz);
vputl(e->memsz); VPUT(e->memsz);
vputl(e->align); VPUT(e->align);
} }
void void
elf64shdr(char *name, Elf64SHdr *e) elf64shdr(char *name, Elf64SHdr *e)
{ {
lputl(e->name); LPUT(e->name);
lputl(e->type); LPUT(e->type);
vputl(e->flags); VPUT(e->flags);
vputl(e->addr); VPUT(e->addr);
vputl(e->off); VPUT(e->off);
vputl(e->size); VPUT(e->size);
lputl(e->link); LPUT(e->link);
lputl(e->info); LPUT(e->info);
vputl(e->addralign); VPUT(e->addralign);
vputl(e->entsize); VPUT(e->entsize);
} }
int int
...@@ -108,7 +107,7 @@ elf64writeshdrs(void) ...@@ -108,7 +107,7 @@ elf64writeshdrs(void)
{ {
int i; int i;
for (i = 0; i < nume64shdr; i++) for (i = 0; i < hdr.shnum; i++)
elf64shdr(sname[i], shdr[i]); elf64shdr(sname[i], shdr[i]);
} }
...@@ -117,7 +116,7 @@ elf64writephdrs(void) ...@@ -117,7 +116,7 @@ elf64writephdrs(void)
{ {
int i; int i;
for (i = 0; i < nume64phdr; i++) for (i = 0; i < hdr.phnum; i++)
elf64phdr(phdr[i]); elf64phdr(phdr[i]);
} }
...@@ -128,10 +127,10 @@ newElf64PHdr(void) ...@@ -128,10 +127,10 @@ newElf64PHdr(void)
e = malloc(sizeof *e); e = malloc(sizeof *e);
memset(e, 0, sizeof *e); memset(e, 0, sizeof *e);
if (nume64phdr >= NSECT) if (hdr.phnum >= NSECT)
diag("too many phdrs"); diag("too many phdrs");
else else
phdr[nume64phdr++] = e; phdr[hdr.phnum++] = e;
return e; return e;
} }
...@@ -140,14 +139,44 @@ newElf64SHdr(char *name) ...@@ -140,14 +139,44 @@ newElf64SHdr(char *name)
{ {
Elf64SHdr *e; Elf64SHdr *e;
if (strcmp(name, ".shstrtab") == 0)
hdr.shstrndx = hdr.shnum;
e = malloc(sizeof *e); e = malloc(sizeof *e);
memset(e, 0, sizeof *e); memset(e, 0, sizeof *e);
e->name = stroffset; e->name = stroffset;
if (nume64shdr >= NSECT) { if (hdr.shnum >= NSECT) {
diag("too many shdrs"); diag("too many shdrs");
} else { } else {
e64addstr(name); e64addstr(name);
shdr[nume64shdr++] = e; shdr[hdr.shnum++] = e;
} }
return 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);
}
...@@ -166,9 +166,11 @@ struct Elf64SHdr ...@@ -166,9 +166,11 @@ struct Elf64SHdr
#define SHF_MASKOS 0x0F000000 /* Environment-specific use */ #define SHF_MASKOS 0x0F000000 /* Environment-specific use */
#define SHF_MASKPROC 0xF0000000 /* Processor-specific use */ #define SHF_MASKPROC 0xF0000000 /* Processor-specific use */
Elf64Hdr *getElf64Hdr();
Elf64SHdr *newElf64SHdr(char*); Elf64SHdr *newElf64SHdr(char*);
Elf64PHdr *newElf64PHdr(); Elf64PHdr *newElf64PHdr();
uint32 elf64headr(void); uint32 elf64headr(void);
void elf64writehdr(void);
void elf64writephdrs(void); void elf64writephdrs(void);
void elf64writeshdrs(void); void elf64writeshdrs(void);
void elf64writestrtable(void); void elf64writestrtable(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment