asm.c 18.2 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 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 67 68 69 70 71 72 73 74 75 76

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
wputl(ushort w)
{
	cput(w);
	cput(w>>8);
}

void
wput(ushort w)
{
	cput(w>>8);
	cput(w);
}

void
77
lput(int32 l)
78 79 80 81 82 83 84 85
{
	cput(l>>24);
	cput(l>>16);
	cput(l>>8);
	cput(l);
}

void
86
vput(vlong v)
87 88 89 90 91 92
{
	lput(v>>32);
	lput(v);
}

void
93
lputl(int32 l)
94 95 96 97 98 99 100
{
	cput(l);
	cput(l>>8);
	cput(l>>16);
	cput(l>>24);
}

Ken Thompson's avatar
Ken Thompson committed
101
void
102
vputl(vlong v)
Ken Thompson's avatar
Ken Thompson committed
103 104 105 106 107
{
	lputl(v);
	lputl(v>>32);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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;
124
	int32 v, magic;
125
	int a, nl, np;
126
	uchar *op1;
Russ Cox's avatar
Russ Cox committed
127
	vlong vl, va, fo, w, symo;
128
	int strtabsize;
Russ Cox's avatar
Russ Cox committed
129
	vlong symdatva = 0x99LL<<32;
130
	Elf64SHdr *sh;
131 132

	strtabsize = 0;
133 134 135 136 137 138 139 140 141 142 143

	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
144
		if(p->pc != pc) {
145 146 147 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
			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
175 176


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

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

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

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	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 */
		lput(magic);			/* magic */
		lput(textsize);			/* sizes */
		lput(datsize);
		lput(bsssize);
		lput(symsize);			/* nsyms */
		vl = entryvalue();
		lput(PADDR(vl));		/* va of entry */
		lput(spsize);			/* sp offsets */
		lput(lcsize);			/* line offsets */
292
		vput(vl);			/* va of entry */
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		break;
	case 3:	/* plan9 */
		magic = 4*26*26+7;
		if(dlm)
			magic |= 0x80000000;
		lput(magic);			/* magic */
		lput(textsize);			/* sizes */
		lput(datsize);
		lput(bsssize);
		lput(symsize);			/* nsyms */
		lput(entryvalue());		/* va of entry */
		lput(spsize);			/* sp offsets */
		lput(lcsize);			/* line offsets */
		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
323
		wputl(40);			/* Shdr size */
324 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
		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 */
362 363 364 365
		nl = 4;
		if (!debug['s'])
			nl += 3;
		if (!debug['d'])	// -d = turn off "dynamic loader"
Russ Cox's avatar
Russ Cox committed
366
			nl += 3;
367
		lputl(nl);			/* number of loads */
368 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
		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 */
405

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

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

417 418
			machsymseg(symo+8,symsize);	/* fileoffset,filesize */
			machsymseg(symo+8+symsize,lcsize);	/* fileoffset,filesize */
419
		}
420
		break;
Ken Thompson's avatar
Ken Thompson committed
421 422 423 424 425 426 427 428
	case 7:
		/* elf amd-64 */
		strnput("\177ELF", 4);		/* e_ident */
		cput(2);			/* class = 64 bit */
		cput(1);			/* data = LSB */
		cput(1);			/* version = CURRENT */
		strnput("", 9);

Ken Thompson's avatar
Ken Thompson committed
429
		wputl(2);			/* type = EXEC */
Ken Thompson's avatar
Ken Thompson committed
430 431
		wputl(62);			/* machine = AMD64 */
		lputl(1L);			/* version = CURRENT */
432 433
		vputl(entryvalue());		/* entry vaddr */
		vputl(64L);			/* offset to first phdr */
Russ Cox's avatar
Russ Cox committed
434 435 436
		np = 3;
		if(!debug['s'])
			np++;
437
		vputl(64L+56*np);		/* offset to first shdr */
Ken Thompson's avatar
Ken Thompson committed
438
		lputl(0L);			/* processor specific flags */
Ken Thompson's avatar
Ken Thompson committed
439 440
		wputl(64);			/* Ehdr size */
		wputl(56);			/* Phdr size */
Russ Cox's avatar
Russ Cox committed
441
		wputl(np);			/* # of Phdrs */
Ken Thompson's avatar
Ken Thompson committed
442
		wputl(64);			/* Shdr size */
443 444 445 446
		if (!debug['s'])
			wputl(7);			/* # of Shdrs */
		else
			wputl(5);			/* # of Shdrs */
Ken Thompson's avatar
Ken Thompson committed
447 448
		wputl(4);			/* Shdr with strings */

449
		fo = 0;
Russ Cox's avatar
Russ Cox committed
450
		va = INITTEXT & ~((vlong)INITRND - 1);
451
		w = HEADR+textsize;
Ken Thompson's avatar
Ken Thompson committed
452

453
		elf64phdr(1,			/* text - type = PT_LOAD */
Ken Thompson's avatar
Ken Thompson committed
454
			1L+4L,			/* text - flags = PF_X+PF_R */
Ken Thompson's avatar
Ken Thompson committed
455 456 457 458 459
			0,			/* file offset */
			va,			/* vaddr */
			va,			/* paddr */
			w,			/* file size */
			w,			/* memory size */
Ken Thompson's avatar
Ken Thompson committed
460
			INITRND);		/* alignment */
Ken Thompson's avatar
Ken Thompson committed
461

462 463 464
		fo = rnd(fo+w, INITRND);
		va = rnd(va+w, INITRND);
		w = datsize;
Ken Thompson's avatar
Ken Thompson committed
465

466
		elf64phdr(1,			/* data - type = PT_LOAD */
Ken Thompson's avatar
Ken Thompson committed
467 468 469 470 471 472
			2L+4L,			/* data - flags = PF_W+PF_R */
			fo,			/* file offset */
			va,			/* vaddr */
			va,			/* paddr */
			w,			/* file size */
			w+bsssize,		/* memory size */
Ken Thompson's avatar
Ken Thompson committed
473 474
			INITRND);		/* alignment */

Russ Cox's avatar
Russ Cox committed
475
		if(!debug['s']) {
476
			elf64phdr(1,			/* data - type = PT_LOAD */
Russ Cox's avatar
Russ Cox committed
477 478 479 480 481 482 483 484 485
				2L+4L,			/* data - flags = PF_W+PF_R */
				symo,		/* file offset */
				symdatva,			/* vaddr */
				symdatva,			/* paddr */
				8+symsize+lcsize,			/* file size */
				8+symsize+lcsize,		/* memory size */
				INITRND);		/* alignment */
		}

486
		elf64phdr(0x6474e551,		/* gok - type = gok */
Russ Cox's avatar
Russ Cox committed
487
			1L+2L+4L,		/* gok - flags = PF_X+PF_W+PF_R */
Ken Thompson's avatar
Ken Thompson committed
488 489 490 491 492 493 494
			0,			/* file offset */
			0,			/* vaddr */
			0,			/* paddr */
			0,			/* file size */
			0,			/* memory size */
			8);			/* alignment */

495 496
		sh = newElf64SHdr();
		elf64shdr(nil, sh);
Ken Thompson's avatar
Ken Thompson committed
497

498
		stroffset = 1;  /* 0 means no name, so start at 1 */
Russ Cox's avatar
Russ Cox committed
499 500 501
		fo = HEADR;
		va = (INITTEXT & ~((vlong)INITRND - 1)) + HEADR;
		w = textsize;
Ken Thompson's avatar
Ken Thompson committed
502

503 504 505 506 507 508 509 510
		sh = newElf64SHdr();
		sh->type = 1;
		sh->flags = 6;
		sh->addr = va;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 8;
		elf64shdr(".text", sh);
Ken Thompson's avatar
Ken Thompson committed
511

512 513 514
		fo = rnd(fo+w, INITRND);
		va = rnd(va+w, INITRND);
		w = datsize;
Ken Thompson's avatar
Ken Thompson committed
515

516 517 518 519 520 521 522 523
		sh = newElf64SHdr();
		sh->type = 1;
		sh->flags = 3;
		sh->addr = va;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 8;
		elf64shdr(".data", sh);
Ken Thompson's avatar
Ken Thompson committed
524

525 526 527
		fo += w;
		va += w;
		w = bsssize;
Ken Thompson's avatar
Ken Thompson committed
528

529 530 531 532 533 534 535 536
		sh = newElf64SHdr();
		sh->type = 8;
		sh->flags = 3;
		sh->addr = va;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 8;
		elf64shdr(".bss", sh);
Ken Thompson's avatar
Ken Thompson committed
537

538
		w = strtabsize;
Ken Thompson's avatar
Ken Thompson committed
539

540 541 542 543 544 545
		sh = newElf64SHdr();
		sh->type = 3;
		sh->off = fo;
		sh->size = w;
		sh->addralign = 1;
		elf64shdr(".shstrtab", sh);
Ken Thompson's avatar
Ken Thompson committed
546

547 548 549
		if (debug['s'])
			break;

Russ Cox's avatar
Russ Cox committed
550
		fo = symo+8;
551 552
		w = symsize;

553 554 555 556 557 558 559
		sh = newElf64SHdr();
		sh->type = 1;	/* type 1 = SHT_PROGBITS */
		sh->off = fo;
		sh->size = w;
		sh->addralign = 1;
		sh->entsize = 24;
		elf64shdr(".gosymtab", sh);
Russ Cox's avatar
Russ Cox committed
560

561 562 563
		fo += w;
		w = lcsize;

564 565 566 567 568 569 570 571
		sh = newElf64SHdr();
		sh->type = 1;	/* type 1 = SHT_PROGBITS */
		sh->off = fo;
		sh->size = w;
		sh->addralign = 1;
		sh->entsize = 24;
		elf64shdr(".gopclntab", sh);

Ken Thompson's avatar
Ken Thompson committed
572
		break;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	}
	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
607
datblk(int32 s, int32 n)
608 609 610
{
	Prog *p;
	uchar *cast;
611
	int32 l, fl, j;
612 613 614 615 616 617 618 619 620 621 622 623
	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
624 625
			i = -l;
			l = 0;
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
		}
		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
672 673 674 675 676 677 678 679 680 681

		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;

682 683 684 685 686 687 688 689
		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);
690 691
					if(p->to.sym->type == Sxxx) {
						curtext = p;	// show useful name in diag's output
Rob Pike's avatar
Rob Pike committed
692
						diag("missing symbol %s", p->to.sym->name);
693
					}
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 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
					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,
764
	uint32 prot1, uint32 prot2, uint32 nsect, uint32 flag)
765
{
766
	lputl(25);	/* segment 64 */
767 768 769 770 771 772 773 774 775 776 777 778
	lputl(72 + 80*nsect);
	strnput(name, 16);
	vputl(vaddr);
	vputl(vsize);
	vputl(foff);
	vputl(fsize);
	lputl(prot1);
	lputl(prot2);
	lputl(nsect);
	lputl(flag);
}

779
void
780
machsymseg(uint32 foffset, uint32 fsize)
781 782 783 784 785 786 787
{
	lputl(3);	/* obsolete gdb debug info */
	lputl(16);	/* size of symseg command */
	lputl(foffset);
	lputl(fsize);
}

788
void
789 790
machsect(char *name, char *seg, vlong addr, vlong size, uint32 off,
	uint32 align, uint32 reloc, uint32 nreloc, uint32 flag)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
{
	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 */
}

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
// 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
821 822 823 824 825
	lputl(2);	/* LC_SYMTAB */
	lputl(24);	/* byte count - 6 words*/
	for(i=0; i<4; i++)
		lputl(0);

826
	lputl(11);	/* LC_DYSYMTAB */
Russ Cox's avatar
Russ Cox committed
827
	lputl(80);	/* byte count - 20 words */
828 829 830 831
	for(i=0; i<18; i++)
		lputl(0);

	lputl(14);	/* LC_LOAD_DYLINKER */
Russ Cox's avatar
Russ Cox committed
832
	lputl(32);	/* byte count */
833
	lputl(12);	/* offset to string */
Russ Cox's avatar
Russ Cox committed
834
	strnput("/usr/lib/dyld", 32-12);
835 836
}

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
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);
}

855
uint32
856 857
machheadr(void)
{
858
	uint32 a;
859 860 861 862 863 864 865 866 867

	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 */
868
	if (!debug['d']) {
Russ Cox's avatar
Russ Cox committed
869
		a += 6;	/* symtab */
870
		a += 20;	/* dysymtab */
Russ Cox's avatar
Russ Cox committed
871
		a += 8;	/* load dylinker */
872
	}
873
	if (!debug['s']) {
Russ Cox's avatar
Russ Cox committed
874
		a += 18;	/* symdat seg */
875 876 877
		a += 4;	/* symtab seg */
		a += 4;	/* lctab seg */
	}
878 879 880

	return a*4;
}