Commit f687fb7e authored by Rob Pike's avatar Rob Pike

6l cleanup: last set of magic numbers for elf64 moved to ld/elf64*.

R=rsc
DELTA=68  (21 added, 28 deleted, 19 changed)
OCL=31857
CL=31864
parent fe734d86
...@@ -196,6 +196,7 @@ asmb(void) ...@@ -196,6 +196,7 @@ asmb(void)
debug['8'] = 1; /* 64-bit addresses */ debug['8'] = 1; /* 64-bit addresses */
v = rnd(HEADR+textsize, INITRND); v = rnd(HEADR+textsize, INITRND);
seek(cout, v, 0); seek(cout, v, 0);
elf64init();
break; break;
} }
...@@ -545,15 +546,14 @@ asmb(void) ...@@ -545,15 +546,14 @@ asmb(void)
eh->machine = 62; /* machine = AMD64 */ eh->machine = 62; /* machine = AMD64 */
eh->version = EV_CURRENT; eh->version = EV_CURRENT;
eh->entry = entryvalue(); eh->entry = entryvalue();
eh->phoff = 64L;
eh->shoff = 64L+56*eh->phnum; a = 0;
eh->ehsize = 64; a += elf64writehdr();
eh->phentsize = 56; a += elf64writephdrs();
eh->shentsize = 64; a += elf64writeshdrs();
if (a > ELF64FULLHDRSIZE) {
elf64writehdr(); diag("ELF64FULLHDRSIZE too small:", a);
elf64writephdrs(); }
elf64writeshdrs();
cflush(); cflush();
/* string table */ /* string table */
......
...@@ -197,7 +197,7 @@ main(int argc, char *argv[]) ...@@ -197,7 +197,7 @@ main(int argc, char *argv[])
INITRND = 4096; INITRND = 4096;
break; break;
case 7: /* elf64 executable */ case 7: /* elf64 executable */
HEADR = elf64headr(); HEADR = ELF64FULLHDRSIZE;
if(INITTEXT == -1) if(INITTEXT == -1)
INITTEXT = (1<<22)+HEADR; INITTEXT = (1<<22)+HEADR;
if(INITDAT == -1) if(INITDAT == -1)
......
...@@ -7,13 +7,23 @@ ...@@ -7,13 +7,23 @@
#include "../ld/elf64.h" #include "../ld/elf64.h"
#define NSECT 16 #define NSECT 16
static int nume64str; static int numstr;
static Elf64Hdr hdr; static Elf64Hdr hdr;
static Elf64PHdr *phdr[NSECT]; static Elf64PHdr *phdr[NSECT];
static Elf64SHdr *shdr[NSECT]; static Elf64SHdr *shdr[NSECT];
static char *sname[NSECT]; static char *sname[NSECT];
static char *str[NSECT]; static char *str[NSECT];
void
elf64init(void)
{
hdr.phoff = ELF64HDRSIZE;
hdr.shoff = ELF64HDRSIZE;
hdr.ehsize = ELF64HDRSIZE;
hdr.phentsize = ELF64PHDRSIZE;
hdr.shentsize = ELF64SHDRSIZE;
}
void void
elf64phdr(Elf64PHdr *e) elf64phdr(Elf64PHdr *e)
{ {
...@@ -56,10 +66,10 @@ void ...@@ -56,10 +66,10 @@ void
elf64writestrtable(void) elf64writestrtable(void)
{ {
int i; int i;
int size; uint32 size;
size = 0; size = 0;
for (i = 0; i < nume64str; i++) for (i = 0; i < numstr; i++)
size += putelf64strtab(str[i]); size += putelf64strtab(str[i]);
if (size > STRTABSIZE) if (size > STRTABSIZE)
diag("elf64 string table overflow"); diag("elf64 string table overflow");
...@@ -68,56 +78,32 @@ elf64writestrtable(void) ...@@ -68,56 +78,32 @@ elf64writestrtable(void)
void void
e64addstr(char *name) e64addstr(char *name)
{ {
if (nume64str >= NSECT) { if (numstr >= NSECT) {
diag("too many elf strings"); diag("too many elf strings");
return; return;
} }
str[nume64str++] = strdup(name); str[numstr++] = strdup(name);
stroffset += strlen(name)+1; stroffset += strlen(name)+1;
} }
uint32 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) elf64writeshdrs(void)
{ {
int i; int i;
for (i = 0; i < hdr.shnum; i++) for (i = 0; i < hdr.shnum; i++)
elf64shdr(sname[i], shdr[i]); elf64shdr(sname[i], shdr[i]);
return hdr.shnum * ELF64SHDRSIZE;
} }
void uint32
elf64writephdrs(void) elf64writephdrs(void)
{ {
int i; int i;
for (i = 0; i < hdr.phnum; i++) for (i = 0; i < hdr.phnum; i++)
elf64phdr(phdr[i]); elf64phdr(phdr[i]);
return hdr.phnum * ELF64PHDRSIZE;
} }
Elf64PHdr* Elf64PHdr*
...@@ -131,6 +117,7 @@ newElf64PHdr(void) ...@@ -131,6 +117,7 @@ newElf64PHdr(void)
diag("too many phdrs"); diag("too many phdrs");
else else
phdr[hdr.phnum++] = e; phdr[hdr.phnum++] = e;
hdr.shoff += ELF64PHDRSIZE;
return e; return e;
} }
...@@ -159,7 +146,7 @@ getElf64Hdr(void) ...@@ -159,7 +146,7 @@ getElf64Hdr(void)
return &hdr; return &hdr;
} }
void uint32
elf64writehdr() elf64writehdr()
{ {
int i; int i;
...@@ -179,4 +166,5 @@ elf64writehdr() ...@@ -179,4 +166,5 @@ elf64writehdr()
WPUT(hdr.shentsize); WPUT(hdr.shentsize);
WPUT(hdr.shnum); WPUT(hdr.shnum);
WPUT(hdr.shstrndx); WPUT(hdr.shstrndx);
return ELF64HDRSIZE;
} }
...@@ -67,6 +67,7 @@ struct Elf64Hdr ...@@ -67,6 +67,7 @@ struct Elf64Hdr
Elf64_Half shnum; /* Number of section header entries */ Elf64_Half shnum; /* Number of section header entries */
Elf64_Half shstrndx; /* Section name string table index */ Elf64_Half shstrndx; /* Section name string table index */
}; };
#define ELF64HDRSIZE 64
/* E ident indexes */ /* E ident indexes */
#define EI_MAG0 0 /* File identification */ #define EI_MAG0 0 /* File identification */
...@@ -112,6 +113,7 @@ struct Elf64PHdr ...@@ -112,6 +113,7 @@ struct Elf64PHdr
Elf64_Xword memsz; /* Size of segment in memory */ Elf64_Xword memsz; /* Size of segment in memory */
Elf64_Xword align; /* Alignment of segment */ Elf64_Xword align; /* Alignment of segment */
}; };
#define ELF64PHDRSIZE 56
/* P types */ /* P types */
#define PT_NULL 0 /* Unused entry */ #define PT_NULL 0 /* Unused entry */
...@@ -140,6 +142,7 @@ struct Elf64SHdr ...@@ -140,6 +142,7 @@ struct Elf64SHdr
Elf64_Xword addralign; /* Address alignment boundary */ Elf64_Xword addralign; /* Address alignment boundary */
Elf64_Xword entsize; /* Size of entries, if section has table */ Elf64_Xword entsize; /* Size of entries, if section has table */
}; };
#define ELF64SHDRSIZE 64
/* S types */ /* S types */
#define SHT_NULL 0 /* Unused section header */ #define SHT_NULL 0 /* Unused section header */
...@@ -166,16 +169,18 @@ struct Elf64SHdr ...@@ -166,16 +169,18 @@ 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 */
void elf64init(void);
Elf64Hdr *getElf64Hdr(); Elf64Hdr *getElf64Hdr();
Elf64SHdr *newElf64SHdr(char*); Elf64SHdr *newElf64SHdr(char*);
Elf64PHdr *newElf64PHdr(); Elf64PHdr *newElf64PHdr();
uint32 elf64headr(void); uint32 elf64writehdr(void);
void elf64writehdr(void); uint32 elf64writephdrs(void);
void elf64writephdrs(void); uint32 elf64writeshdrs(void);
void elf64writeshdrs(void);
void elf64writestrtable(void); void elf64writestrtable(void);
extern int nume64phdr; extern int nume64phdr;
extern int nume64shdr; extern int nume64shdr;
#define STRTABSIZE 256 #define STRTABSIZE 256
/* Amount of space to reserve at the start of the file; may waste some */
#define ELF64FULLHDRSIZE 2048
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