1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:04:47 -07:00

more elf64 support: phdrs, constants

R=rsc
DELTA=130  (68 added, 6 deleted, 56 changed)
OCL=31798
CL=31798
This commit is contained in:
Rob Pike 2009-07-17 15:09:17 -07:00
parent f95a42e6ba
commit e86dcf1603
3 changed files with 122 additions and 60 deletions

View File

@ -127,6 +127,7 @@ asmb(void)
vlong vl, va, fo, w, symo; vlong vl, va, fo, w, symo;
int strtabsize; int strtabsize;
vlong symdatva = 0x99LL<<32; vlong symdatva = 0x99LL<<32;
Elf64PHdr *ph;
Elf64SHdr *sh; Elf64SHdr *sh;
strtabsize = 0; strtabsize = 0;
@ -450,47 +451,49 @@ asmb(void)
va = INITTEXT & ~((vlong)INITRND - 1); va = INITTEXT & ~((vlong)INITRND - 1);
w = HEADR+textsize; w = HEADR+textsize;
elf64phdr(1, /* text - type = PT_LOAD */ ph = newElf64PHdr();
1L+4L, /* text - flags = PF_X+PF_R */ ph->type = PT_LOAD;
0, /* file offset */ ph->flags = PF_X+PF_R;
va, /* vaddr */ ph->vaddr = va;
va, /* paddr */ ph->paddr = va;
w, /* file size */ ph->filesz = w;
w, /* memory size */ ph->memsz = w;
INITRND); /* alignment */ ph->align = INITRND;
elf64phdr(ph);
fo = rnd(fo+w, INITRND); fo = rnd(fo+w, INITRND);
va = rnd(va+w, INITRND); va = rnd(va+w, INITRND);
w = datsize; w = datsize;
elf64phdr(1, /* data - type = PT_LOAD */ ph = newElf64PHdr();
2L+4L, /* data - flags = PF_W+PF_R */ ph->type = PT_LOAD;
fo, /* file offset */ ph->flags = PF_W+PF_R;
va, /* vaddr */ ph->off = fo;
va, /* paddr */ ph->vaddr = va;
w, /* file size */ ph->paddr = va;
w+bsssize, /* memory size */ ph->filesz = w;
INITRND); /* alignment */ ph->memsz = w+bsssize;
ph->align = INITRND;
elf64phdr(ph);
if(!debug['s']) { if(!debug['s']) {
elf64phdr(1, /* data - type = PT_LOAD */ ph = newElf64PHdr();
2L+4L, /* data - flags = PF_W+PF_R */ ph->type = PT_LOAD;
symo, /* file offset */ ph->flags = PF_W+PF_R;
symdatva, /* vaddr */ ph->off = symo;
symdatva, /* paddr */ ph->vaddr = symdatva;
8+symsize+lcsize, /* file size */ ph->paddr = symdatva;
8+symsize+lcsize, /* memory size */ ph->filesz = 8+symsize+lcsize;
INITRND); /* alignment */ ph->memsz = 8+symsize+lcsize;
ph->align = INITRND;
elf64phdr(ph);
} }
elf64phdr(0x6474e551, /* gok - type = gok */ ph = newElf64PHdr();
1L+2L+4L, /* gok - flags = PF_X+PF_W+PF_R */ ph->type = 0x6474e551; /* gok */
0, /* file offset */ ph->flags = PF_X+PF_W+PF_R;
0, /* vaddr */ ph->align = 8;
0, /* paddr */ elf64phdr(ph);
0, /* file size */
0, /* memory size */
8); /* alignment */
sh = newElf64SHdr(); sh = newElf64SHdr();
elf64shdr(nil, sh); elf64shdr(nil, sh);
@ -501,8 +504,8 @@ asmb(void)
w = textsize; w = textsize;
sh = newElf64SHdr(); sh = newElf64SHdr();
sh->type = 1; sh->type = SHT_PROGBITS;
sh->flags = 6; sh->flags = SHF_ALLOC+SHF_EXECINSTR;
sh->addr = va; sh->addr = va;
sh->off = fo; sh->off = fo;
sh->size = w; sh->size = w;
@ -514,8 +517,8 @@ asmb(void)
w = datsize; w = datsize;
sh = newElf64SHdr(); sh = newElf64SHdr();
sh->type = 1; sh->type = SHT_PROGBITS;
sh->flags = 3; sh->flags = SHF_WRITE+SHF_ALLOC;
sh->addr = va; sh->addr = va;
sh->off = fo; sh->off = fo;
sh->size = w; sh->size = w;
@ -527,8 +530,8 @@ asmb(void)
w = bsssize; w = bsssize;
sh = newElf64SHdr(); sh = newElf64SHdr();
sh->type = 8; sh->type = SHT_NOBITS;
sh->flags = 3; sh->flags = SHF_WRITE+SHF_ALLOC;
sh->addr = va; sh->addr = va;
sh->off = fo; sh->off = fo;
sh->size = w; sh->size = w;
@ -538,7 +541,7 @@ asmb(void)
w = strtabsize; w = strtabsize;
sh = newElf64SHdr(); sh = newElf64SHdr();
sh->type = 3; sh->type = SHT_STRTAB;
sh->off = fo; sh->off = fo;
sh->size = w; sh->size = w;
sh->addralign = 1; sh->addralign = 1;
@ -551,7 +554,7 @@ asmb(void)
w = symsize; w = symsize;
sh = newElf64SHdr(); sh = newElf64SHdr();
sh->type = 1; /* type 1 = SHT_PROGBITS */ sh->type = SHT_PROGBITS;
sh->off = fo; sh->off = fo;
sh->size = w; sh->size = w;
sh->addralign = 1; sh->addralign = 1;
@ -562,7 +565,7 @@ asmb(void)
w = lcsize; w = lcsize;
sh = newElf64SHdr(); sh = newElf64SHdr();
sh->type = 1; /* type 1 = SHT_PROGBITS */ sh->type = SHT_PROGBITS;
sh->off = fo; sh->off = fo;
sh->size = w; sh->size = w;
sh->addralign = 1; sh->addralign = 1;

View File

@ -7,19 +7,16 @@
#include "../ld/elf64.h" #include "../ld/elf64.h"
void void
elf64phdr(int type, int flags, vlong foff, elf64phdr(Elf64PHdr *e)
vlong vaddr, vlong paddr,
vlong filesize, vlong memsize, vlong align)
{ {
lputl(e->type);
lputl(type); /* type */ lputl(e->flags);
lputl(flags); /* flags */ vputl(e->off);
vputl(foff); /* file offset */ vputl(e->vaddr);
vputl(vaddr); /* vaddr */ vputl(e->paddr);
vputl(paddr); /* paddr */ vputl(e->filesz);
vputl(filesize); /* file size */ vputl(e->memsz);
vputl(memsize); /* memory size */ vputl(e->align);
vputl(align); /* alignment */
} }
void void
@ -106,3 +103,14 @@ newElf64SHdr()
return e; return e;
} }
Elf64PHdr*
newElf64PHdr()
{
Elf64PHdr *e;
e = malloc(sizeof *e);
memset(e, 0, sizeof *e);
return e;
}

View File

@ -47,6 +47,7 @@ typedef int64 Elf64_Sxword; /* Signed long integer */
typedef struct Elf64Hdr Elf64Hdr; typedef struct Elf64Hdr Elf64Hdr;
typedef struct Elf64SHdr Elf64SHdr; typedef struct Elf64SHdr Elf64SHdr;
typedef struct Elf64PHdr Elf64PHdr;
struct Elf64Hdr struct Elf64Hdr
{ {
@ -66,6 +67,32 @@ struct Elf64Hdr
Elf64_Half shstrndx; /* Section name string table index */ Elf64_Half shstrndx; /* Section name string table index */
}; };
struct Elf64PHdr
{
Elf64_Word type; /* Type of segment */
Elf64_Word flags; /* Segment attributes */
Elf64_Off off; /* Offset in file */
Elf64_Addr vaddr; /* Virtual address in memory */
Elf64_Addr paddr; /* Reserved */
Elf64_Xword filesz; /* Size of segment in file */
Elf64_Xword memsz; /* Size of segment in memory */
Elf64_Xword align; /* Alignment of segment */
};
/* P types */
#define PT_NULL 0 /* Unused entry */
#define PT_LOAD 1 /* Loadable segment */
#define PT_DYNAMIC 2 /* Dynamic linking tables */
#define PT_INTERP 3 /* Program interpreter path name */
#define PT_NOTE 4 /* Note sections */
/* P flags */
#define PF_X 0x1 /* Execute permission */
#define PF_W 0x2 /* Write permission */
#define PF_R 0x4 /* Read permission */
#define PF_MASKOS 0x00FF0000 /* reserved for environment-specific use */
#define PF_MASKPROC 0xFF000000 /*reserved for processor-specific use */
struct Elf64SHdr struct Elf64SHdr
{ {
Elf64_Word name; /* Section name */ Elf64_Word name; /* Section name */
@ -80,10 +107,34 @@ struct Elf64SHdr
Elf64_Xword entsize; /* Size of entries, if section has table */ Elf64_Xword entsize; /* Size of entries, if section has table */
}; };
/* S types */
#define SHT_NULL 0 /* Unused section header */
#define SHT_PROGBITS 1 /* Information defined by the program */
#define SHT_SYMTAB 2 /* Linker symbol table */
#define SHT_STRTAB 3 /* String table */
#define SHT_RELA 4 /* "Rela" type relocation entries */
#define SHT_HASH 5 /* Symbol hash table */
#define SHT_DYNAMIC 6 /* Dynamic linking tables */
#define SHT_NOTE 7 /* Note information */
#define SHT_NOBITS 8 /* Uninitialized space; does not occupy any space in the file */
#define SHT_REL 9 /* "Rel" type relocation entries */
#define SHT_SHLIB 10 /* Reserved */
#define SHT_DYNSYM 11 /* A dynamic loader symbol table */
#define SHT_LOOS 0x60000000 /* Environment-specific use */
#define SHT_HIOS 0x6FFFFFFF
#define SHT_LOPROC 0x70000000 /* Processor-specific use */
#define SHT_HIPROC 0x7FFFFFFF
/* S flags */
#define SHF_WRITE 0x1 /* Writable data */
#define SHF_ALLOC 0x2 /* Allocated in memory image of program */
#define SHF_EXECINSTR 0x4 /* Executable instructions */
#define SHF_MASKOS 0x0F000000 /* Environment-specific use */
#define SHF_MASKPROC 0xF0000000 /* Processor-specific use */
Elf64SHdr *newElf64SHdr(); Elf64SHdr *newElf64SHdr();
Elf64PHdr *newElf64PHdr();
uint32 elf64headr(void); uint32 elf64headr(void);
void elf64phdr(int type, int flags, vlong foff, void elf64phdr(Elf64PHdr*);
vlong vaddr, vlong paddr,
vlong filesize, vlong memsize, vlong align);
void elf64shdr(char*, Elf64SHdr*); void elf64shdr(char*, Elf64SHdr*);
int elf64strtable(void); int elf64strtable(void);