elf.c 3.69 KB
Newer Older
1 2 3 4
// 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.

Russ Cox's avatar
Russ Cox committed
5
#include "../ld/elf.h"
6

Russ Cox's avatar
Russ Cox committed
7 8 9 10 11
/*
 * 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.
 */
12
#define	NSECT	16
Russ Cox's avatar
Russ Cox committed
13 14 15 16 17

static	int	elf64;
static	Elf64_Ehdr	hdr;
static	Elf64_Phdr	*phdr[NSECT];
static	Elf64_Shdr	*shdr[NSECT];
18 19
static	char	*sname[NSECT];

20 21 22 23
/*
 Initialize the global variable that describes the ELF header. It will be updated as
 we write section and prog headers.
 */
24
void
Russ Cox's avatar
Russ Cox committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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 */

	}
47 48
}

49
void
Russ Cox's avatar
Russ Cox committed
50
elf64phdr(Elf64_Phdr *e)
51
{
Rob Pike's avatar
Rob Pike committed
52 53 54 55 56 57 58 59
	LPUT(e->type);
	LPUT(e->flags);
	VPUT(e->off);
	VPUT(e->vaddr);
	VPUT(e->paddr);
	VPUT(e->filesz);
	VPUT(e->memsz);
	VPUT(e->align);
60 61 62
}

void
Russ Cox's avatar
Russ Cox committed
63
elf64shdr(char *name, Elf64_Shdr *e)
64
{
Rob Pike's avatar
Rob Pike committed
65 66 67 68 69 70 71 72 73 74
	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);
75 76 77
}

uint32
78
elf64writeshdrs(void)
79
{
80
	int i;
81

Rob Pike's avatar
Rob Pike committed
82
	for (i = 0; i < hdr.shnum; i++)
83
		elf64shdr(sname[i], shdr[i]);
84
	return hdr.shnum * ELF64SHDRSIZE;
85 86
}

87
uint32
88 89 90 91
elf64writephdrs(void)
{
	int i;

Rob Pike's avatar
Rob Pike committed
92
	for (i = 0; i < hdr.phnum; i++)
93
		elf64phdr(phdr[i]);
94
	return hdr.phnum * ELF64PHDRSIZE;
95
}
96

Russ Cox's avatar
Russ Cox committed
97 98
Elf64_Phdr*
newElf64_Phdr(void)
99
{
Russ Cox's avatar
Russ Cox committed
100
	Elf64_Phdr *e;
101 102 103

	e = malloc(sizeof *e);
	memset(e, 0, sizeof *e);
Rob Pike's avatar
Rob Pike committed
104
	if (hdr.phnum >= NSECT)
105 106
		diag("too many phdrs");
	else
Rob Pike's avatar
Rob Pike committed
107
		phdr[hdr.phnum++] = e;
108
	hdr.shoff += ELF64PHDRSIZE;
109 110 111
	return e;
}

Russ Cox's avatar
Russ Cox committed
112 113
Elf64_Shdr*
newElf64_Shstrtab(vlong name)
114
{
Russ Cox's avatar
Russ Cox committed
115 116 117 118 119 120 121 122
	hdr.shstrndx = hdr.shnum;
	return newElf64_Shdr(name);
}

Elf64_Shdr*
newElf64_Shdr(vlong name)
{
	Elf64_Shdr *e;
123 124 125

	e = malloc(sizeof *e);
	memset(e, 0, sizeof *e);
Russ Cox's avatar
Russ Cox committed
126
	e->name = name;
Rob Pike's avatar
Rob Pike committed
127
	if (hdr.shnum >= NSECT) {
128 129
		diag("too many shdrs");
	} else {
Rob Pike's avatar
Rob Pike committed
130
		shdr[hdr.shnum++] = e;
131 132 133
	}
	return e;
}
Rob Pike's avatar
Rob Pike committed
134

Russ Cox's avatar
Russ Cox committed
135 136
Elf64_Ehdr*
getElf64_Ehdr(void)
Rob Pike's avatar
Rob Pike committed
137 138 139 140
{
	return &hdr;
}

141
uint32
Russ Cox's avatar
Russ Cox committed
142
elf64writehdr(void)
Rob Pike's avatar
Rob Pike committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
	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);
161
	return ELF64HDRSIZE;
Rob Pike's avatar
Rob Pike committed
162
}
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

/* 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
Russ Cox's avatar
Russ Cox committed
179
elfwritedynent(Sym *s, int tag, uint64 val)
180
{
Russ Cox's avatar
Russ Cox committed
181 182 183 184 185 186 187
	if(elf64) {
		adduint64(s, tag);
		adduint64(s, val);
	} else {
		adduint32(s, tag);
		adduint32(s, val);
	}
188 189
}

Russ Cox's avatar
Russ Cox committed
190 191
void
elfwritedynentsym(Sym *s, int tag, Sym *t)
192
{
Russ Cox's avatar
Russ Cox committed
193 194 195 196 197
	if(elf64)
		adduint64(s, tag);
	else
		adduint32(s, tag);
	addaddr(s, t);
198 199
}

Russ Cox's avatar
Russ Cox committed
200 201
void
elfwritedynentsymsize(Sym *s, int tag, Sym *t)
202
{
Russ Cox's avatar
Russ Cox committed
203 204 205 206 207
	if(elf64)
		adduint64(s, tag);
	else
		adduint32(s, tag);
	addsize(s, t);
208
}