2009-07-17 15:32:37 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
#include "../ld/elf.h"
|
2009-07-17 15:32:37 -06:00
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
/*
|
|
|
|
* We use the 64-bit data structures on both 32- and 64-bit machines
|
|
|
|
* in order to write the code just once. The 64-bit data structure is
|
|
|
|
* written in the 32-bit format on the 32-bit machines.
|
|
|
|
*/
|
2009-07-17 17:34:58 -06:00
|
|
|
#define NSECT 16
|
2009-08-21 14:08:11 -06:00
|
|
|
|
|
|
|
static int elf64;
|
|
|
|
static Elf64_Ehdr hdr;
|
|
|
|
static Elf64_Phdr *phdr[NSECT];
|
|
|
|
static Elf64_Shdr *shdr[NSECT];
|
2009-07-17 17:34:58 -06:00
|
|
|
static char *sname[NSECT];
|
|
|
|
|
2009-07-21 16:10:47 -06:00
|
|
|
/*
|
|
|
|
Initialize the global variable that describes the ELF header. It will be updated as
|
|
|
|
we write section and prog headers.
|
|
|
|
*/
|
2009-07-20 14:19:47 -06:00
|
|
|
void
|
2009-08-21 14:08:11 -06:00
|
|
|
elfinit(void)
|
|
|
|
{
|
|
|
|
switch(thechar) {
|
|
|
|
// 64-bit architectures
|
|
|
|
case '6':
|
|
|
|
elf64 = 1;
|
|
|
|
hdr.phoff = ELF64HDRSIZE; /* Must be be ELF64HDRSIZE: first PHdr must follow ELF header */
|
|
|
|
hdr.shoff = ELF64HDRSIZE; /* Will move as we add PHeaders */
|
|
|
|
hdr.ehsize = ELF64HDRSIZE; /* Must be ELF64HDRSIZE */
|
|
|
|
hdr.phentsize = ELF64PHDRSIZE; /* Must be ELF64PHDRSIZE */
|
|
|
|
hdr.shentsize = ELF64SHDRSIZE; /* Must be ELF64SHDRSIZE */
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 32-bit architectures
|
|
|
|
default:
|
|
|
|
hdr.phoff = ELF32HDRSIZE; /* Must be be ELF32HDRSIZE: first PHdr must follow ELF header */
|
|
|
|
hdr.shoff = ELF32HDRSIZE; /* Will move as we add PHeaders */
|
|
|
|
hdr.ehsize = ELF32HDRSIZE; /* Must be ELF32HDRSIZE */
|
|
|
|
hdr.phentsize = ELF32PHDRSIZE; /* Must be ELF32PHDRSIZE */
|
|
|
|
hdr.shentsize = ELF32SHDRSIZE; /* Must be ELF32SHDRSIZE */
|
|
|
|
|
|
|
|
}
|
2009-07-20 14:19:47 -06:00
|
|
|
}
|
|
|
|
|
2009-07-17 15:32:37 -06:00
|
|
|
void
|
2009-08-21 14:08:11 -06:00
|
|
|
elf64phdr(Elf64_Phdr *e)
|
2009-07-17 15:32:37 -06:00
|
|
|
{
|
2009-07-20 11:08:48 -06:00
|
|
|
LPUT(e->type);
|
|
|
|
LPUT(e->flags);
|
|
|
|
VPUT(e->off);
|
|
|
|
VPUT(e->vaddr);
|
|
|
|
VPUT(e->paddr);
|
|
|
|
VPUT(e->filesz);
|
|
|
|
VPUT(e->memsz);
|
|
|
|
VPUT(e->align);
|
2009-07-17 15:32:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-21 14:08:11 -06:00
|
|
|
elf64shdr(char *name, Elf64_Shdr *e)
|
2009-07-17 15:32:37 -06:00
|
|
|
{
|
2009-07-20 11:08:48 -06:00
|
|
|
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);
|
2009-07-17 15:32:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32
|
2009-07-17 17:34:58 -06:00
|
|
|
elf64writeshdrs(void)
|
2009-07-17 15:32:37 -06:00
|
|
|
{
|
2009-07-17 17:34:58 -06:00
|
|
|
int i;
|
2009-07-17 15:32:37 -06:00
|
|
|
|
2009-07-20 11:08:48 -06:00
|
|
|
for (i = 0; i < hdr.shnum; i++)
|
2009-07-17 17:34:58 -06:00
|
|
|
elf64shdr(sname[i], shdr[i]);
|
2009-07-20 14:19:47 -06:00
|
|
|
return hdr.shnum * ELF64SHDRSIZE;
|
2009-07-17 15:32:37 -06:00
|
|
|
}
|
|
|
|
|
2009-07-20 14:19:47 -06:00
|
|
|
uint32
|
2009-07-17 17:34:58 -06:00
|
|
|
elf64writephdrs(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2009-07-20 11:08:48 -06:00
|
|
|
for (i = 0; i < hdr.phnum; i++)
|
2009-07-17 17:34:58 -06:00
|
|
|
elf64phdr(phdr[i]);
|
2009-07-20 14:19:47 -06:00
|
|
|
return hdr.phnum * ELF64PHDRSIZE;
|
2009-07-17 17:34:58 -06:00
|
|
|
}
|
2009-07-17 16:09:17 -06:00
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
Elf64_Phdr*
|
|
|
|
newElf64_Phdr(void)
|
2009-07-17 16:09:17 -06:00
|
|
|
{
|
2009-08-21 14:08:11 -06:00
|
|
|
Elf64_Phdr *e;
|
2009-07-17 16:09:17 -06:00
|
|
|
|
|
|
|
e = malloc(sizeof *e);
|
|
|
|
memset(e, 0, sizeof *e);
|
2009-07-20 11:08:48 -06:00
|
|
|
if (hdr.phnum >= NSECT)
|
2009-07-17 17:34:58 -06:00
|
|
|
diag("too many phdrs");
|
|
|
|
else
|
2009-07-20 11:08:48 -06:00
|
|
|
phdr[hdr.phnum++] = e;
|
2009-07-20 14:19:47 -06:00
|
|
|
hdr.shoff += ELF64PHDRSIZE;
|
2009-07-17 16:09:17 -06:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
Elf64_Shdr*
|
|
|
|
newElf64_Shstrtab(vlong name)
|
2009-07-17 17:34:58 -06:00
|
|
|
{
|
2009-08-21 14:08:11 -06:00
|
|
|
hdr.shstrndx = hdr.shnum;
|
|
|
|
return newElf64_Shdr(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Elf64_Shdr*
|
|
|
|
newElf64_Shdr(vlong name)
|
|
|
|
{
|
|
|
|
Elf64_Shdr *e;
|
2009-07-17 17:34:58 -06:00
|
|
|
|
|
|
|
e = malloc(sizeof *e);
|
|
|
|
memset(e, 0, sizeof *e);
|
2009-08-21 14:08:11 -06:00
|
|
|
e->name = name;
|
2009-07-20 11:08:48 -06:00
|
|
|
if (hdr.shnum >= NSECT) {
|
2009-07-17 17:34:58 -06:00
|
|
|
diag("too many shdrs");
|
|
|
|
} else {
|
2009-07-20 11:08:48 -06:00
|
|
|
shdr[hdr.shnum++] = e;
|
2009-07-17 17:34:58 -06:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
2009-07-20 11:08:48 -06:00
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
Elf64_Ehdr*
|
|
|
|
getElf64_Ehdr(void)
|
2009-07-20 11:08:48 -06:00
|
|
|
{
|
|
|
|
return &hdr;
|
|
|
|
}
|
|
|
|
|
2009-07-20 14:19:47 -06:00
|
|
|
uint32
|
2009-08-20 17:09:38 -06:00
|
|
|
elf64writehdr(void)
|
2009-07-20 11:08:48 -06:00
|
|
|
{
|
|
|
|
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);
|
2009-07-20 14:19:47 -06:00
|
|
|
return ELF64HDRSIZE;
|
2009-07-20 11:08:48 -06:00
|
|
|
}
|
2009-07-21 16:10:47 -06:00
|
|
|
|
|
|
|
/* Taken directly from the definition document for ELF64 */
|
|
|
|
uint32
|
|
|
|
elf64_hash(uchar *name)
|
|
|
|
{
|
|
|
|
unsigned long h = 0, g;
|
|
|
|
while (*name) {
|
|
|
|
h = (h << 4) + *name++;
|
|
|
|
if (g = h & 0xf0000000)
|
|
|
|
h ^= g >> 24;
|
|
|
|
h &= 0x0fffffff;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-21 14:08:11 -06:00
|
|
|
elfwritedynent(Sym *s, int tag, uint64 val)
|
2009-07-21 16:10:47 -06:00
|
|
|
{
|
2009-08-21 14:08:11 -06:00
|
|
|
if(elf64) {
|
|
|
|
adduint64(s, tag);
|
|
|
|
adduint64(s, val);
|
|
|
|
} else {
|
|
|
|
adduint32(s, tag);
|
|
|
|
adduint32(s, val);
|
|
|
|
}
|
2009-07-21 16:10:47 -06:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
void
|
|
|
|
elfwritedynentsym(Sym *s, int tag, Sym *t)
|
2009-07-21 16:10:47 -06:00
|
|
|
{
|
2009-08-21 14:08:11 -06:00
|
|
|
if(elf64)
|
|
|
|
adduint64(s, tag);
|
|
|
|
else
|
|
|
|
adduint32(s, tag);
|
|
|
|
addaddr(s, t);
|
2009-07-21 16:10:47 -06:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:08:11 -06:00
|
|
|
void
|
|
|
|
elfwritedynentsymsize(Sym *s, int tag, Sym *t)
|
2009-07-21 16:10:47 -06:00
|
|
|
{
|
2009-08-21 14:08:11 -06:00
|
|
|
if(elf64)
|
|
|
|
adduint64(s, tag);
|
|
|
|
else
|
|
|
|
adduint32(s, tag);
|
|
|
|
addsize(s, t);
|
2009-07-21 16:10:47 -06:00
|
|
|
}
|