asm.c 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Inferno utils/6l/asm.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/asm.c
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 Bruce Ellis
//	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include	"l.h"
32
#include	"../ld/elf64.h"
33 34 35

#define	Dbufslop	100

36
#define PADDR(a)	((uint32)(a) & ~0x80000000)
37

38 39 40 41
char linuxdynld[] = "/lib64/ld-linux-x86-64.so.2";

char	zeroes[32];

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
vlong
entryvalue(void)
{
	char *a;
	Sym *s;

	a = INITENTRY;
	if(*a >= '0' && *a <= '9')
		return atolwhex(a);
	s = lookup(a, 0);
	if(s->type == 0)
		return INITTEXT;
	switch(s->type) {
	case STEXT:
		break;
	case SDATA:
		if(dlm)
			return s->value+INITDAT;
	default:
		diag("entry not text: %s", s->name);
	}
	return s->value;
}

void
Rob Pike's avatar
Rob Pike committed
67
wputl(uint16 w)
68 69 70 71 72 73
{
	cput(w);
	cput(w>>8);
}

void
Rob Pike's avatar
Rob Pike committed
74
wputb(uint16 w)
75 76 77 78 79 80
{
	cput(w>>8);
	cput(w);
}

void
Rob Pike's avatar
Rob Pike committed
81
lputb(int32 l)
82 83 84 85 86 87 88 89
{
	cput(l>>24);
	cput(l>>16);
	cput(l>>8);
	cput(l);
}

void
Rob Pike's avatar
Rob Pike committed
90
vputb(uint64 v)
91
{
Rob Pike's avatar
Rob Pike committed
92 93
	lputb(v>>32);
	lputb(v);
94 95 96
}

void
97
lputl(int32 l)
98 99 100 101 102 103 104
{
	cput(l);
	cput(l>>8);
	cput(l>>16);
	cput(l>>24);
}

Ken Thompson's avatar
Ken Thompson committed
105
void
Rob Pike's avatar
Rob Pike committed
106
vputl(uint64 v)
Ken Thompson's avatar
Ken Thompson committed
107 108 109 110 111
{
	lputl(v);
	lputl(v>>32);
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
void
strnput(char *s, int n)
{
	int i;

	for(i=0; i<n; i++) {
		cput(*s);
		if(*s != 0)
			s++;
	}
}

void
asmb(void)
{
	Prog *p;
128
	int32 v, magic;
129
	int a, nl;
130
	uchar *op1;
131
	vlong vl, va, startva, fo, w, symo, hashoff;
Russ Cox's avatar
Russ Cox committed
132
	vlong symdatva = 0x99LL<<32;
Rob Pike's avatar
Rob Pike committed
133
	Elf64Hdr *eh;
134
	Elf64PHdr *ph, *pph;
Russ Cox's avatar
Russ Cox committed
135
	Elf64SHdr *sh;
136

137 138 139 140 141 142 143 144 145 146
	if(debug['v'])
		Bprint(&bso, "%5.2f asmb\n", cputime());
	Bflush(&bso);

	seek(cout, HEADR, 0);
	pc = INITTEXT;
	curp = firstp;
	for(p = firstp; p != P; p = p->link) {
		if(p->as == ATEXT)
			curtext = p;
Ken Thompson's avatar
Ken Thompson committed
147
		if(p->pc != pc) {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
			if(!debug['a'])
				print("%P\n", curp);
			diag("phase error %llux sb %llux in %s", p->pc, pc, TNAME);
			pc = p->pc;
		}
		curp = p;
		asmins(p);
		a = (andptr - and);
		if(cbc < a)
			cflush();
		if(debug['a']) {
			Bprint(&bso, pcstr, pc);
			for(op1 = and; op1 < andptr; op1++)
				Bprint(&bso, "%.2ux", *op1);
			for(; op1 < and+Maxand; op1++)
				Bprint(&bso, "  ");
			Bprint(&bso, "%P\n", curp);
		}
		if(dlm) {
			if(p->as == ATEXT)
				reloca = nil;
			else if(reloca != nil)
				diag("reloc failure: %P", curp);
		}
		memmove(cbp, and, a);
		cbp += a;
		pc += a;
		cbc -= a;
	}
	cflush();
Ken Thompson's avatar
Ken Thompson committed
178 179


180 181 182 183 184 185 186 187
	switch(HEADTYPE) {
	default:
		diag("unknown header type %ld", HEADTYPE);
	case 2:
	case 5:
		seek(cout, HEADR+textsize, 0);
		break;
	case 6:
188
		debug['8'] = 1;	/* 64-bit addresses */
189
		v = HEADR+textsize;
190
		seek(cout, v, 0);
191 192 193 194 195 196 197
		v = rnd(v, 4096) - v;
		while(v > 0) {
			cput(0);
			v--;
		}
		cflush();
		break;
Ken Thompson's avatar
Ken Thompson committed
198

Ken Thompson's avatar
Ken Thompson committed
199
	case 7:
200
		debug['8'] = 1;	/* 64-bit addresses */
Ken Thompson's avatar
Ken Thompson committed
201
		v = rnd(HEADR+textsize, INITRND);
202
		seek(cout, v, 0);
203
		elf64init();
Ken Thompson's avatar
Ken Thompson committed
204
		break;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	}

	if(debug['v'])
		Bprint(&bso, "%5.2f datblk\n", cputime());
	Bflush(&bso);

	if(dlm){
		char buf[8];

		write(cout, buf, INITDAT-textsize);
		textsize = INITDAT;
	}

	for(v = 0; v < datsize; v += sizeof(buf)-Dbufslop) {
		if(datsize-v > sizeof(buf)-Dbufslop)
			datblk(v, sizeof(buf)-Dbufslop);
		else
			datblk(v, datsize-v);
	}

	symsize = 0;
	spsize = 0;
	lcsize = 0;
Russ Cox's avatar
Russ Cox committed
228
	symo = 0;
229 230 231 232 233 234 235 236
	if(!debug['s']) {
		if(debug['v'])
			Bprint(&bso, "%5.2f sym\n", cputime());
		Bflush(&bso);
		switch(HEADTYPE) {
		default:
		case 2:
		case 5:
237
			debug['s'] = 1;
Russ Cox's avatar
Russ Cox committed
238
			symo = HEADR+textsize+datsize;
Ken Thompson's avatar
Ken Thompson committed
239
			break;
240
		case 6:
Russ Cox's avatar
Russ Cox committed
241
			symo = rnd(HEADR+textsize, INITRND)+rnd(datsize, INITRND);
242
			break;
243
		case 7:
244
			symo = rnd(HEADR+textsize, INITRND)+datsize;
Russ Cox's avatar
Russ Cox committed
245
			symo = rnd(symo, INITRND);
246
			break;
247
		}
Russ Cox's avatar
Russ Cox committed
248
		seek(cout, symo+8, 0);
249 250 251 252 253 254 255 256 257 258 259 260 261
		if(!debug['s'])
			asmsym();
		if(debug['v'])
			Bprint(&bso, "%5.2f sp\n", cputime());
		Bflush(&bso);
		if(debug['v'])
			Bprint(&bso, "%5.2f pc\n", cputime());
		Bflush(&bso);
		if(!debug['s'])
			asmlc();
		if(dlm)
			asmdyn();
		cflush();
Russ Cox's avatar
Russ Cox committed
262 263 264 265
		seek(cout, symo, 0);
		lputl(symsize);
		lputl(lcsize);
		cflush();
Ken Thompson's avatar
Ken Thompson committed
266 267
	} else
	if(dlm){
268 269 270 271
		seek(cout, HEADR+textsize+datsize, 0);
		asmdyn();
		cflush();
	}
Ken Thompson's avatar
Ken Thompson committed
272

273 274 275 276 277 278 279 280 281 282 283
	if(debug['v'])
		Bprint(&bso, "%5.2f headr\n", cputime());
	Bflush(&bso);
	seek(cout, 0L, 0);
	switch(HEADTYPE) {
	default:
	case 2:	/* plan9 */
		magic = 4*26*26+7;
		magic |= 0x00008000;		/* fat header */
		if(dlm)
			magic |= 0x80000000;	/* dlm */
Rob Pike's avatar
Rob Pike committed
284 285 286 287 288
		lputb(magic);			/* magic */
		lputb(textsize);			/* sizes */
		lputb(datsize);
		lputb(bsssize);
		lputb(symsize);			/* nsyms */
289
		vl = entryvalue();
Rob Pike's avatar
Rob Pike committed
290 291 292 293
		lputb(PADDR(vl));		/* va of entry */
		lputb(spsize);			/* sp offsets */
		lputb(lcsize);			/* line offsets */
		vputb(vl);			/* va of entry */
294 295 296 297 298
		break;
	case 3:	/* plan9 */
		magic = 4*26*26+7;
		if(dlm)
			magic |= 0x80000000;
Rob Pike's avatar
Rob Pike committed
299 300 301 302 303 304 305 306
		lputb(magic);			/* magic */
		lputb(textsize);		/* sizes */
		lputb(datsize);
		lputb(bsssize);
		lputb(symsize);			/* nsyms */
		lputb(entryvalue());		/* va of entry */
		lputb(spsize);			/* sp offsets */
		lputb(lcsize);			/* line offsets */
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
		break;
	case 5:
		strnput("\177ELF", 4);		/* e_ident */
		cput(1);			/* class = 32 bit */
		cput(1);			/* data = LSB */
		cput(1);			/* version = CURRENT */
		strnput("", 9);
		wputl(2);			/* type = EXEC */
		wputl(62);			/* machine = AMD64 */
		lputl(1L);			/* version = CURRENT */
		lputl(PADDR(entryvalue()));	/* entry vaddr */
		lputl(52L);			/* offset to first phdr */
		lputl(0L);			/* offset to first shdr */
		lputl(0L);			/* processor specific flags */
		wputl(52);			/* Ehdr size */
		wputl(32);			/* Phdr size */
		wputl(3);			/* # of Phdrs */
Ken Thompson's avatar
Ken Thompson committed
324
		wputl(40);			/* Shdr size */
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		wputl(0);			/* # of Shdrs */
		wputl(0);			/* Shdr string size */

		lputl(1L);			/* text - type = PT_LOAD */
		lputl(HEADR);			/* file offset */
		lputl(INITTEXT);		/* vaddr */
		lputl(PADDR(INITTEXT));		/* paddr */
		lputl(textsize);		/* file size */
		lputl(textsize);		/* memory size */
		lputl(0x05L);			/* protections = RX */
		lputl(INITRND);			/* alignment */

		lputl(1L);			/* data - type = PT_LOAD */
		lputl(HEADR+textsize);		/* file offset */
		lputl(INITDAT);			/* vaddr */
		lputl(PADDR(INITDAT));		/* paddr */
		lputl(datsize);			/* file size */
		lputl(datsize+bsssize);		/* memory size */
		lputl(0x06L);			/* protections = RW */
		lputl(INITRND);			/* alignment */

		lputl(0L);			/* data - type = PT_NULL */
		lputl(HEADR+textsize+datsize);	/* file offset */
		lputl(0L);
		lputl(0L);
		lputl(symsize);			/* symbol table size */
		lputl(lcsize);			/* line number size */
		lputl(0x04L);			/* protections = R */
		lputl(0x04L);			/* alignment */
		break;
	case 6:
		/* apple MACH */
		va = 4096;

		lputl(0xfeedfacf);		/* 64-bit */
		lputl((1<<24)|7);		/* cputype - x86/ABI64 */
		lputl(3);			/* subtype - x86 */
		lputl(2);			/* file type - mach executable */
363 364 365 366
		nl = 4;
		if (!debug['s'])
			nl += 3;
		if (!debug['d'])	// -d = turn off "dynamic loader"
Russ Cox's avatar
Russ Cox committed
367
			nl += 3;
368
		lputl(nl);			/* number of loads */
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
		lputl(machheadr()-32);		/* size of loads */
		lputl(1);			/* flags - no undefines */
		lputl(0);			/* reserved */

		machseg("__PAGEZERO",
			0,va,			/* vaddr vsize */
			0,0,			/* fileoffset filesize */
			0,0,			/* protects */
			0,0);			/* sections flags */

		v = rnd(HEADR+textsize, INITRND);
		machseg("__TEXT",
			va,			/* vaddr */
			v,			/* vsize */
			0,v,			/* fileoffset filesize */
			7,5,			/* protects */
			1,0);			/* sections flags */
		machsect("__text", "__TEXT",
			va+HEADR,v-HEADR,	/* addr size */
			HEADR,0,0,0,		/* offset align reloc nreloc */
			0|0x400);		/* flag - some instructions */

		w = datsize+bsssize;
		machseg("__DATA",
			va+v,			/* vaddr */
			w,			/* vsize */
			v,datsize,		/* fileoffset filesize */
			7,3,			/* protects */
			2,0);			/* sections flags */
		machsect("__data", "__DATA",
			va+v,datsize,		/* addr size */
			v,0,0,0,		/* offset align reloc nreloc */
			0);			/* flag */
		machsect("__bss", "__DATA",
			va+v+datsize,bsssize,	/* addr size */
			0,0,0,0,		/* offset align reloc nreloc */
			1);			/* flag - zero fill */
406

407
		machdylink();
408
		machstack(entryvalue());
409 410

		if (!debug['s']) {
Russ Cox's avatar
Russ Cox committed
411 412 413 414 415 416 417
			machseg("__SYMDAT",
				symdatva,		/* vaddr */
				8+symsize+lcsize,		/* vsize */
				symo, 8+symsize+lcsize,	/* fileoffset filesize */
				7, 5,			/* protects */
				0, 0);			/* sections flags */

418 419
			machsymseg(symo+8,symsize);	/* fileoffset,filesize */
			machsymseg(symo+8+symsize,lcsize);	/* fileoffset,filesize */
420
		}
421
		break;
Ken Thompson's avatar
Ken Thompson committed
422 423
	case 7:
		/* elf amd-64 */
Ken Thompson's avatar
Ken Thompson committed
424

425
		fo = 0;
426 427
		startva = INITTEXT - HEADR;
		va = startva;
428
		w = HEADR+textsize;
Ken Thompson's avatar
Ken Thompson committed
429

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
		/* This null SHdr must appear before all others */
		sh = newElf64SHdr("");

		pph = nil;	/* silence compiler */

		/* Dynamic linking sections */
		if (!debug['d']) {	/* -d suppresses dynamic loader format */

			/* P headers */
			/* program header info */
			pph = newElf64PHdr();
			pph->type = PT_PHDR;
			pph->flags = PF_R + PF_X;
			pph->off = ELF64HDRSIZE;
			pph->vaddr = startva + pph->off;
			pph->paddr = startva + pph->off;
			pph->align = 8;

			/* interpreter */
			ph = newElf64PHdr();
			ph->type = PT_INTERP;
			ph->flags = PF_R;
			ph->off = startelf();
453 454
			ph->vaddr = startva + ph->off;
			ph->paddr = startva + ph->off;
455 456 457 458
			write(cout, linuxdynld, sizeof linuxdynld);
			ph->filesz = endelf() - ph->off;
			ph->align = 1;

Russ Cox's avatar
Russ Cox committed
459
			/* S header for interpreter */
460 461 462 463 464 465 466 467
			sh = newElf64SHdr(".interp");
			sh->type = SHT_PROGBITS;
			sh->flags = SHF_ALLOC;
			sh->off = ph->off;
			sh->addr = startva + sh->off;
			sh->size = ph->filesz;
			sh->addralign = 1;

468 469 470 471 472
			/* S headers inside dynamic load section */
			sh = newElf64SHdr(".hash");
			sh->type = SHT_HASH;
			sh->flags = SHF_ALLOC;
			sh->entsize = 4;
Russ Cox's avatar
Russ Cox committed
473
			sh->off = startelf();
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
			hashoff = sh->off;
			sh->addr = startva + sh->off;
			/* temporary hack: 8 zeroes means 0 buckets, 0 chains */
			write(cout, zeroes, 8);
			sh->size = endelf() - sh->off;
			sh->addralign = 8;

			sh = newElf64SHdr(".got");
			sh->type = SHT_PROGBITS;
			sh->flags = SHF_ALLOC+SHF_WRITE;
			sh->entsize = 8;
			sh->off = startelf();
			sh->addr = startva + sh->off;
			sh->size = endelf() - sh->off;
			sh->addralign = 8;

			sh = newElf64SHdr(".got.plt");
			sh->type = SHT_PROGBITS;
			sh->flags = SHF_ALLOC+SHF_WRITE;
			sh->entsize = 8;
			sh->off = startelf();
			sh->addr = startva + sh->off;
			sh->size = endelf() - sh->off;
			sh->addralign = 8;

Russ Cox's avatar
Russ Cox committed
499 500 501 502 503 504
			sh = newElf64SHdr(".dynamic");
			sh->type = SHT_DYNAMIC;
			sh->flags = SHF_ALLOC+SHF_WRITE;
			sh->entsize = 16;
			sh->addr = startva + sh->off;
			sh->off = startelf();
505 506 507 508 509 510 511 512 513
			elf64writedynent(DT_HASH, startva+hashoff);
			elf64writedynent(DT_STRTAB, startva+ELF64FULLHDRSIZE-STRTABSIZE);
			elf64writedynent(DT_SYMTAB, startva);
			elf64writedynent(DT_RELA, startva);
			elf64writedynent(DT_RELASZ, 0);	// size of the whole rela in bytes
			elf64writedynent(DT_RELAENT, ELF64RELASIZE);
			elf64writedynent(DT_STRSZ, STRTABSIZE);
			elf64writedynent(DT_SYMENT, 0);
			elf64writedynent(DT_NULL, 0);
Russ Cox's avatar
Russ Cox committed
514 515
			sh->size = endelf() - sh->off;
			sh->addralign = 8;
516

Russ Cox's avatar
Russ Cox committed
517
			/* PT_DYNAMIC for .dynamic section */
518 519 520
			ph = newElf64PHdr();
			ph->type = PT_DYNAMIC;
			ph->flags = PF_R + PF_W;
Russ Cox's avatar
Russ Cox committed
521 522 523 524 525 526 527 528 529 530 531 532
			ph->off = sh->off;
			ph->vaddr = startva + ph->off;
			ph->paddr = startva + ph->off;
			ph->filesz = sh->size;
			ph->memsz = sh->size;
			ph->align = 8;

			/* PT_LOAD for all dynamic sections */
			ph = newElf64PHdr();
			ph->type = PT_LOAD;
			ph->flags = PF_R + PF_W;
			ph->off = 0;
533 534
			ph->vaddr = startva + ph->off;
			ph->paddr = startva + ph->off;
Russ Cox's avatar
Russ Cox committed
535 536
			ph->filesz = sh->off + sh->size - ph->off;
			ph->memsz = ph->filesz;
537 538 539
			ph->align = 8;
		}

540
		ph = newElf64PHdr();
541
		ph->type = PT_LOAD;
542
		ph->flags = PF_X+PF_R;
543 544 545 546 547
		ph->vaddr = va + ELF64RESERVE;
		ph->paddr = va + ELF64RESERVE;
		ph->off = ELF64RESERVE;
		ph->filesz = w - ELF64RESERVE;
		ph->memsz = w - ELF64RESERVE;
548
		ph->align = INITRND;
Ken Thompson's avatar
Ken Thompson committed
549

550 551 552
		fo = rnd(fo+w, INITRND);
		va = rnd(va+w, INITRND);
		w = datsize;
Ken Thompson's avatar
Ken Thompson committed
553

554 555 556 557 558 559 560 561 562
		ph = newElf64PHdr();
		ph->type = PT_LOAD;
		ph->flags = PF_W+PF_R;
		ph->off = fo;
		ph->vaddr = va;
		ph->paddr = va;
		ph->filesz = w;
		ph->memsz = w+bsssize;
		ph->align = INITRND;
Ken Thompson's avatar
Ken Thompson committed
563

Russ Cox's avatar
Russ Cox committed
564
		if(!debug['s']) {
565 566 567 568 569 570 571 572 573
			ph = newElf64PHdr();
			ph->type = PT_LOAD;
			ph->flags = PF_W+PF_R;
			ph->off = symo;
			ph->vaddr = symdatva;
			ph->paddr = symdatva;
			ph->filesz = 8+symsize+lcsize;
			ph->memsz = 8+symsize+lcsize;
			ph->align = INITRND;
Russ Cox's avatar
Russ Cox committed
574 575
		}

576
		ph = newElf64PHdr();
577 578
		ph->type = 0x6474e551; 	/* GNU_STACK */
		ph->flags = PF_W+PF_R;
579
		ph->align = 8;
Ken Thompson's avatar
Ken Thompson committed
580

581 582
		fo = ELF64RESERVE;
		va = startva + fo;
Russ Cox's avatar
Russ Cox committed
583
		w = textsize;
Ken Thompson's avatar
Ken Thompson committed
584

585
		sh = newElf64SHdr(".text");
586 587
		sh->type = SHT_PROGBITS;
		sh->flags = SHF_ALLOC+SHF_EXECINSTR;
588 589 590 591
		sh->addr = va;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 8;
Ken Thompson's avatar
Ken Thompson committed
592

593 594 595
		fo = rnd(fo+w, INITRND);
		va = rnd(va+w, INITRND);
		w = datsize;
Ken Thompson's avatar
Ken Thompson committed
596

597
		sh = newElf64SHdr(".data");
598 599
		sh->type = SHT_PROGBITS;
		sh->flags = SHF_WRITE+SHF_ALLOC;
600 601 602 603
		sh->addr = va;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 8;
Ken Thompson's avatar
Ken Thompson committed
604

605 606 607
		fo += w;
		va += w;
		w = bsssize;
Ken Thompson's avatar
Ken Thompson committed
608

609
		sh = newElf64SHdr(".bss");
610 611
		sh->type = SHT_NOBITS;
		sh->flags = SHF_WRITE+SHF_ALLOC;
612 613 614 615
		sh->addr = va;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 8;
Ken Thompson's avatar
Ken Thompson committed
616

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		if (!debug['s']) {
			fo = symo+8;
			w = symsize;

			sh = newElf64SHdr(".gosymtab");
			sh->type = SHT_PROGBITS;
			sh->off = fo;
			sh->size = w;
			sh->addralign = 1;
			sh->entsize = 24;

			fo += w;
			w = lcsize;

			sh = newElf64SHdr(".gopclntab");
			sh->type = SHT_PROGBITS;
			sh->off = fo;
			sh->size = w;
			sh->addralign = 1;
			sh->entsize = 24;
		}
Ken Thompson's avatar
Ken Thompson committed
638

639
		sh = newElf64SHdr(".shstrtab");
640
		sh->type = SHT_STRTAB;
641 642
		sh->off = startelf();
		sh->addr = sh->off + startva;
643
		sh->addralign = 1;
644 645
		elf64writestrtable();
		sh->size = endelf() - sh->off;
646

647
		/* Main header */
Rob Pike's avatar
Rob Pike committed
648 649 650 651 652 653 654 655 656 657 658 659 660
		eh = getElf64Hdr();
		eh->ident[EI_MAG0] = '\177';
		eh->ident[EI_MAG1] = 'E';
		eh->ident[EI_MAG2] = 'L';
		eh->ident[EI_MAG3] = 'F';
		eh->ident[EI_CLASS] = ELFCLASS64;
		eh->ident[EI_DATA] = ELFDATA2LSB;
		eh->ident[EI_VERSION] = EV_CURRENT;

		eh->type = ET_EXEC;
		eh->machine = 62;	/* machine = AMD64 */
		eh->version = EV_CURRENT;
		eh->entry = entryvalue();
661

662 663 664 665 666 667
		if (!debug['d']) {
			pph->filesz = eh->phnum * ELF64PHDRSIZE;
			pph->memsz = pph->filesz;
		}

		seek(cout, 0, 0);
668 669 670 671 672 673 674
		a = 0;
		a += elf64writehdr();
		a += elf64writephdrs();
		a += elf64writeshdrs();
		if (a > ELF64FULLHDRSIZE) {
			diag("ELF64FULLHDRSIZE too small:", a);
		}
675 676
		cflush();

Ken Thompson's avatar
Ken Thompson committed
677
		break;
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	}
	cflush();
}

void
cflush(void)
{
	int n;

	n = sizeof(buf.cbuf) - cbc;
	if(n)
		write(cout, buf.cbuf, n);
	cbp = buf.cbuf;
	cbc = sizeof(buf.cbuf);
}

void
outa(int n, uchar *cast, uchar *map, vlong l)
{
	int i, j;

	Bprint(&bso, pcstr, l);
	for(i=0; i<n; i++) {
		j = i;
		if(map != nil)
			j = map[j];
		Bprint(&bso, "%.2ux", cast[j]);
	}
	for(; i<Maxand; i++)
		Bprint(&bso, "  ");
	Bprint(&bso, "%P\n", curp);
}

void
712
datblk(int32 s, int32 n)
713 714 715
{
	Prog *p;
	uchar *cast;
716
	int32 l, fl, j;
717 718 719 720 721 722 723 724 725 726 727 728
	vlong o;
	int i, c;

	memset(buf.dbuf, 0, n+Dbufslop);
	for(p = datap; p != P; p = p->link) {
		curp = p;
		l = p->from.sym->value + p->from.offset - s;
		c = p->from.scale;
		i = 0;
		if(l < 0) {
			if(l+c <= 0)
				continue;
Russ Cox's avatar
Russ Cox committed
729 730
			i = -l;
			l = 0;
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
		}
		if(l >= n)
			continue;
		if(p->as != AINIT && p->as != ADYNT) {
			for(j=l+(c-i)-1; j>=l; j--)
				if(buf.dbuf[j]) {
					print("%P\n", p);
					diag("multiple initialization");
					break;
				}
		}

		switch(p->to.type) {
		case D_FCONST:
			switch(c) {
			default:
			case 4:
				fl = ieeedtof(&p->to.ieee);
				cast = (uchar*)&fl;
				if(debug['a'] && i == 0)
					outa(c, cast, fnuxi4, l+s+INITDAT);
				for(; i<c; i++) {
					buf.dbuf[l] = cast[fnuxi4[i]];
					l++;
				}
				break;
			case 8:
				cast = (uchar*)&p->to.ieee;
				if(debug['a'] && i == 0)
					outa(c, cast, fnuxi8, l+s+INITDAT);
				for(; i<c; i++) {
					buf.dbuf[l] = cast[fnuxi8[i]];
					l++;
				}
				break;
			}
			break;

		case D_SCONST:
			if(debug['a'] && i == 0)
				outa(c, (uchar*)p->to.scon, nil, l+s+INITDAT);
			for(; i<c; i++) {
				buf.dbuf[l] = p->to.scon[i];
				l++;
			}
			break;
Russ Cox's avatar
Russ Cox committed
777 778 779 780 781 782 783 784 785 786

		case D_SBIG:
			if(debug['a'] && i == 0)
				outa(c, (uchar*)p->to.sbig, nil, l+s+INITDAT);
			for(; i<c; i++) {
				buf.dbuf[l] = p->to.sbig[i];
				l++;
			}
			break;

787 788 789 790 791 792 793 794
		default:
			o = p->to.offset;
			if(p->to.type == D_ADDR) {
				if(p->to.index != D_STATIC && p->to.index != D_EXTERN)
					diag("DADDR type%P", p);
				if(p->to.sym) {
					if(p->to.sym->type == SUNDEF)
						ckoff(p->to.sym, o);
795 796
					if(p->to.sym->type == Sxxx) {
						curtext = p;	// show useful name in diag's output
Rob Pike's avatar
Rob Pike committed
797
						diag("missing symbol %s", p->to.sym->name);
798
					}
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
					o += p->to.sym->value;
					if(p->to.sym->type != STEXT && p->to.sym->type != SUNDEF)
						o += INITDAT;
					if(dlm)
						dynreloc(p->to.sym, l+s+INITDAT, 1);
				}
			}
			fl = o;
			cast = (uchar*)&fl;
			switch(c) {
			default:
				diag("bad nuxi %d %d\n%P", c, i, curp);
				break;
			case 1:
				if(debug['a'] && i == 0)
					outa(c, cast, inuxi1, l+s+INITDAT);
				for(; i<c; i++) {
					buf.dbuf[l] = cast[inuxi1[i]];
					l++;
				}
				break;
			case 2:
				if(debug['a'] && i == 0)
					outa(c, cast, inuxi2, l+s+INITDAT);
				for(; i<c; i++) {
					buf.dbuf[l] = cast[inuxi2[i]];
					l++;
				}
				break;
			case 4:
				if(debug['a'] && i == 0)
					outa(c, cast, inuxi4, l+s+INITDAT);
				for(; i<c; i++) {
					buf.dbuf[l] = cast[inuxi4[i]];
					l++;
				}
				break;
			case 8:
				cast = (uchar*)&o;
				if(debug['a'] && i == 0)
					outa(c, cast, inuxi8, l+s+INITDAT);
				for(; i<c; i++) {
					buf.dbuf[l] = cast[inuxi8[i]];
					l++;
				}
				break;
			}
			break;
		}
	}
	write(cout, buf.dbuf, n);
}

vlong
rnd(vlong v, vlong r)
{
	vlong c;

	if(r <= 0)
		return v;
	v += r - 1;
	c = v % r;
	if(c < 0)
		c += r;
	v -= c;
	return v;
}

void
machseg(char *name, vlong vaddr, vlong vsize, vlong foff, vlong fsize,
869
	uint32 prot1, uint32 prot2, uint32 nsect, uint32 flag)
870
{
871
	lputl(25);	/* segment 64 */
872 873 874 875 876 877 878 879 880 881 882 883
	lputl(72 + 80*nsect);
	strnput(name, 16);
	vputl(vaddr);
	vputl(vsize);
	vputl(foff);
	vputl(fsize);
	lputl(prot1);
	lputl(prot2);
	lputl(nsect);
	lputl(flag);
}

884
void
885
machsymseg(uint32 foffset, uint32 fsize)
886 887 888 889 890 891 892
{
	lputl(3);	/* obsolete gdb debug info */
	lputl(16);	/* size of symseg command */
	lputl(foffset);
	lputl(fsize);
}

893
void
894 895
machsect(char *name, char *seg, vlong addr, vlong size, uint32 off,
	uint32 align, uint32 reloc, uint32 nreloc, uint32 flag)
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
{
	strnput(name, 16);
	strnput(seg, 16);
	vputl(addr);
	vputl(size);
	lputl(off);
	lputl(align);
	lputl(reloc);
	lputl(nreloc);
	lputl(flag);
	lputl(0);	/* reserved */
	lputl(0);	/* reserved */
	lputl(0);	/* reserved */
}

911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
// Emit a section requesting the dynamic loader
// but giving it no work to do (an empty dynamic symbol table).
// This is enough to make the Apple tracing programs (like dtrace)
// accept the binary, so that one can run dtruss on a 6.out.
// The dynamic linker loads at 0x8fe00000, so if we want to
// be able to build >2GB binaries, we're going to need to move
// the text segment to 4G like Apple does.
void
machdylink(void)
{
	int i;

	if(debug['d'])
		return;

Russ Cox's avatar
Russ Cox committed
926 927 928 929 930
	lputl(2);	/* LC_SYMTAB */
	lputl(24);	/* byte count - 6 words*/
	for(i=0; i<4; i++)
		lputl(0);

931
	lputl(11);	/* LC_DYSYMTAB */
Russ Cox's avatar
Russ Cox committed
932
	lputl(80);	/* byte count - 20 words */
933 934 935 936
	for(i=0; i<18; i++)
		lputl(0);

	lputl(14);	/* LC_LOAD_DYLINKER */
Russ Cox's avatar
Russ Cox committed
937
	lputl(32);	/* byte count */
938
	lputl(12);	/* offset to string */
Russ Cox's avatar
Russ Cox committed
939
	strnput("/usr/lib/dyld", 32-12);
940 941
}

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
void
machstack(vlong e)
{
	int i;

	lputl(5);			/* unix thread */
	lputl((42+4)*4);		/* total byte count */

	lputl(4);			/* thread type */
	lputl(42);			/* word count */

	for(i=0; i<32; i++)
		lputl(0);
	vputl(e);
	for(i=0; i<8; i++)
		lputl(0);
}

960
uint32
961 962
machheadr(void)
{
963
	uint32 a;
964 965 966 967 968 969 970 971 972

	a = 8;		/* a.out header */
	a += 18;	/* page zero seg */
	a += 18;	/* text seg */
	a += 20;	/* text sect */
	a += 18;	/* data seg */
	a += 20;	/* data sect */
	a += 20;	/* bss sect */
	a += 46;	/* stack sect */
973
	if (!debug['d']) {
Russ Cox's avatar
Russ Cox committed
974
		a += 6;	/* symtab */
975
		a += 20;	/* dysymtab */
Russ Cox's avatar
Russ Cox committed
976
		a += 8;	/* load dylinker */
977
	}
978
	if (!debug['s']) {
Russ Cox's avatar
Russ Cox committed
979
		a += 18;	/* symdat seg */
980 981 982
		a += 4;	/* symtab seg */
		a += 4;	/* lctab seg */
	}
983 984 985

	return a*4;
}