From f687fb7e7379c73e0050982f9e1b4848ff689787 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Mon, 20 Jul 2009 13:19:47 -0700 Subject: [PATCH] 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 --- src/cmd/6l/asm.c | 16 +++++++------- src/cmd/6l/obj.c | 2 +- src/cmd/ld/elf64.c | 54 ++++++++++++++++++---------------------------- src/cmd/ld/elf64.h | 13 +++++++---- 4 files changed, 39 insertions(+), 46 deletions(-) diff --git a/src/cmd/6l/asm.c b/src/cmd/6l/asm.c index 3db010a788..c1d0a4d97a 100644 --- a/src/cmd/6l/asm.c +++ b/src/cmd/6l/asm.c @@ -196,6 +196,7 @@ asmb(void) debug['8'] = 1; /* 64-bit addresses */ v = rnd(HEADR+textsize, INITRND); seek(cout, v, 0); + elf64init(); break; } @@ -545,15 +546,14 @@ asmb(void) 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 */ diff --git a/src/cmd/6l/obj.c b/src/cmd/6l/obj.c index a2a520396f..8538e064e4 100644 --- a/src/cmd/6l/obj.c +++ b/src/cmd/6l/obj.c @@ -197,7 +197,7 @@ main(int argc, char *argv[]) INITRND = 4096; break; case 7: /* elf64 executable */ - HEADR = elf64headr(); + HEADR = ELF64FULLHDRSIZE; if(INITTEXT == -1) INITTEXT = (1<<22)+HEADR; if(INITDAT == -1) diff --git a/src/cmd/ld/elf64.c b/src/cmd/ld/elf64.c index 9b191693d9..ea1a39b418 100644 --- a/src/cmd/ld/elf64.c +++ b/src/cmd/ld/elf64.c @@ -7,13 +7,23 @@ #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) { @@ -56,10 +66,10 @@ void 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"); @@ -68,56 +78,32 @@ elf64writestrtable(void) 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* @@ -131,6 +117,7 @@ newElf64PHdr(void) diag("too many phdrs"); else phdr[hdr.phnum++] = e; + hdr.shoff += ELF64PHDRSIZE; return e; } @@ -159,7 +146,7 @@ getElf64Hdr(void) return &hdr; } -void +uint32 elf64writehdr() { int i; @@ -179,4 +166,5 @@ elf64writehdr() WPUT(hdr.shentsize); WPUT(hdr.shnum); WPUT(hdr.shstrndx); + return ELF64HDRSIZE; } diff --git a/src/cmd/ld/elf64.h b/src/cmd/ld/elf64.h index fc71744e64..f42ae94252 100644 --- a/src/cmd/ld/elf64.h +++ b/src/cmd/ld/elf64.h @@ -67,6 +67,7 @@ struct Elf64Hdr 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 */ @@ -112,6 +113,7 @@ struct Elf64PHdr 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 */ @@ -140,6 +142,7 @@ struct Elf64SHdr 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 */ @@ -166,16 +169,18 @@ struct Elf64SHdr #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