10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*580Swesolows * Common Development and Distribution License (the "License"). 6*580Swesolows * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*580Swesolows * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Bootstrap the linker/loader. 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/bootconf.h> 340Sstevel@tonic-gate #include <sys/link.h> 350Sstevel@tonic-gate #include <sys/auxv.h> 360Sstevel@tonic-gate #include <sys/kobj.h> 370Sstevel@tonic-gate #include <sys/elf.h> 380Sstevel@tonic-gate #include <sys/bootsvcs.h> 390Sstevel@tonic-gate #include <sys/kobj_impl.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate #if !defined(__GNUC__) 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * We don't use the global offset table, but 450Sstevel@tonic-gate * ld may throw in an UNDEFINED reference in 460Sstevel@tonic-gate * our symbol table. 470Sstevel@tonic-gate */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate #pragma weak _GLOBAL_OFFSET_TABLE_ 500Sstevel@tonic-gate 510Sstevel@tonic-gate #else 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * We -do- use the global offset table, but only by 550Sstevel@tonic-gate * accident -- when you tell gcc to emit PIC code, 560Sstevel@tonic-gate * it -always- generates a reference to the GOT in 570Sstevel@tonic-gate * a register, even if the compilation unit never 580Sstevel@tonic-gate * uses it. 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * Rumoured to be fixed in a later version of gcc.. 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate 630Sstevel@tonic-gate long _GLOBAL_OFFSET_TABLE_[1]; 640Sstevel@tonic-gate 650Sstevel@tonic-gate #endif 660Sstevel@tonic-gate 670Sstevel@tonic-gate #define MASK(n) ((1<<(n))-1) 680Sstevel@tonic-gate #define IN_RANGE(v, n) ((-(1<<((n)-1))) <= (v) && (v) < (1<<((n)-1))) 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define roundup ALIGN 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * Boot transfers control here. At this point, 740Sstevel@tonic-gate * we haven't relocated our own symbols, so the 750Sstevel@tonic-gate * world (as we know it) is pretty small right now. 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate void 780Sstevel@tonic-gate _kobj_boot( 790Sstevel@tonic-gate struct boot_syscalls *syscallp, 800Sstevel@tonic-gate void *dvec, 810Sstevel@tonic-gate struct bootops *bootops, 820Sstevel@tonic-gate Boot *ebp) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate Shdr *section[24]; /* cache */ 850Sstevel@tonic-gate val_t bootaux[BA_NUM]; 860Sstevel@tonic-gate struct bootops *bop; 870Sstevel@tonic-gate Phdr *phdr; 880Sstevel@tonic-gate auxv_t *auxv = NULL; 890Sstevel@tonic-gate Shdr *sh; 900Sstevel@tonic-gate Half sh_num; 910Sstevel@tonic-gate uint_t end, edata = 0; 920Sstevel@tonic-gate int i; 930Sstevel@tonic-gate 940Sstevel@tonic-gate bop = (dvec) ? *(struct bootops **)bootops : bootops; 950Sstevel@tonic-gate 960Sstevel@tonic-gate for (i = 0; i < BA_NUM; i++) 970Sstevel@tonic-gate bootaux[i].ba_val = NULL; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Check the bootstrap vector. 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate for (; ebp->eb_tag != EB_NULL; ebp++) { 1030Sstevel@tonic-gate switch (ebp->eb_tag) { 1040Sstevel@tonic-gate case EB_AUXV: 1050Sstevel@tonic-gate auxv = (auxv_t *)ebp->eb_un.eb_ptr; 1060Sstevel@tonic-gate break; 1070Sstevel@tonic-gate case EB_DYNAMIC: 1080Sstevel@tonic-gate bootaux[BA_DYNAMIC].ba_ptr = (void *)ebp->eb_un.eb_ptr; 1090Sstevel@tonic-gate break; 1100Sstevel@tonic-gate default: 1110Sstevel@tonic-gate break; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate if (auxv == NULL) 1160Sstevel@tonic-gate return; 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * Now the aux vector. 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate for (; auxv->a_type != AT_NULL; auxv++) { 1220Sstevel@tonic-gate switch (auxv->a_type) { 1230Sstevel@tonic-gate case AT_PHDR: 1240Sstevel@tonic-gate bootaux[BA_PHDR].ba_ptr = auxv->a_un.a_ptr; 1250Sstevel@tonic-gate break; 1260Sstevel@tonic-gate case AT_PHENT: 1270Sstevel@tonic-gate bootaux[BA_PHENT].ba_val = auxv->a_un.a_val; 1280Sstevel@tonic-gate break; 1290Sstevel@tonic-gate case AT_PHNUM: 1300Sstevel@tonic-gate bootaux[BA_PHNUM].ba_val = auxv->a_un.a_val; 1310Sstevel@tonic-gate break; 1320Sstevel@tonic-gate case AT_PAGESZ: 1330Sstevel@tonic-gate bootaux[BA_PAGESZ].ba_val = auxv->a_un.a_val; 1340Sstevel@tonic-gate break; 1350Sstevel@tonic-gate case AT_SUN_LDELF: 1360Sstevel@tonic-gate bootaux[BA_LDELF].ba_ptr = auxv->a_un.a_ptr; 1370Sstevel@tonic-gate break; 1380Sstevel@tonic-gate case AT_SUN_LDSHDR: 1390Sstevel@tonic-gate bootaux[BA_LDSHDR].ba_ptr = auxv->a_un.a_ptr; 1400Sstevel@tonic-gate break; 1410Sstevel@tonic-gate case AT_SUN_LDNAME: 1420Sstevel@tonic-gate bootaux[BA_LDNAME].ba_ptr = auxv->a_un.a_ptr; 1430Sstevel@tonic-gate break; 1440Sstevel@tonic-gate case AT_SUN_LPAGESZ: 1450Sstevel@tonic-gate bootaux[BA_LPAGESZ].ba_val = auxv->a_un.a_val; 1460Sstevel@tonic-gate break; 1470Sstevel@tonic-gate case AT_SUN_CPU: 1480Sstevel@tonic-gate bootaux[BA_CPU].ba_ptr = auxv->a_un.a_ptr; 1490Sstevel@tonic-gate break; 1500Sstevel@tonic-gate case AT_SUN_MMU: 1510Sstevel@tonic-gate bootaux[BA_MMU].ba_ptr = auxv->a_un.a_ptr; 1520Sstevel@tonic-gate break; 1530Sstevel@tonic-gate case AT_ENTRY: 1540Sstevel@tonic-gate bootaux[BA_ENTRY].ba_ptr = auxv->a_un.a_ptr; 1550Sstevel@tonic-gate break; 1560Sstevel@tonic-gate default: 1570Sstevel@tonic-gate break; 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate sh = (Shdr *)bootaux[BA_LDSHDR].ba_ptr; 1620Sstevel@tonic-gate sh_num = ((Ehdr *)bootaux[BA_LDELF].ba_ptr)->e_shnum; 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Build cache table for section addresses. 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate for (i = 0; i < sh_num; i++) { 1670Sstevel@tonic-gate section[i] = sh++; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * Find the end of data 1720Sstevel@tonic-gate * (to allocate bss) 1730Sstevel@tonic-gate */ 1740Sstevel@tonic-gate phdr = (Phdr *)bootaux[BA_PHDR].ba_ptr; 1750Sstevel@tonic-gate for (i = 0; i < bootaux[BA_PHNUM].ba_val; i++) { 1760Sstevel@tonic-gate if (phdr->p_type == PT_LOAD && 1770Sstevel@tonic-gate (phdr->p_flags & PF_W) && (phdr->p_flags & PF_X)) { 1780Sstevel@tonic-gate edata = end = phdr->p_vaddr + phdr->p_memsz; 1790Sstevel@tonic-gate break; 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate phdr = (Phdr *)((ulong_t)phdr + bootaux[BA_PHENT].ba_val); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate if (edata == NULL) 1840Sstevel@tonic-gate return; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * Find the symbol table, and then loop 1880Sstevel@tonic-gate * through the symbols adjusting their 1890Sstevel@tonic-gate * values to reflect where the sections 1900Sstevel@tonic-gate * were loaded. 1910Sstevel@tonic-gate */ 1920Sstevel@tonic-gate for (i = 1; i < sh_num; i++) { 1930Sstevel@tonic-gate Shdr *shp; 1940Sstevel@tonic-gate Sym *sp; 1950Sstevel@tonic-gate uint_t off; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate shp = section[i]; 1980Sstevel@tonic-gate if (shp->sh_type != SHT_SYMTAB) 1990Sstevel@tonic-gate continue; 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate for (off = 0; off < shp->sh_size; off += shp->sh_entsize) { 2020Sstevel@tonic-gate sp = (Sym *)(shp->sh_addr + off); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (sp->st_shndx == SHN_ABS || 2050Sstevel@tonic-gate sp->st_shndx == SHN_UNDEF) 2060Sstevel@tonic-gate continue; 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * Assign the addresses for COMMON 2090Sstevel@tonic-gate * symbols even though we haven't 2100Sstevel@tonic-gate * actually allocated bss yet. 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate if (sp->st_shndx == SHN_COMMON) { 2130Sstevel@tonic-gate end = ALIGN(end, sp->st_value); 2140Sstevel@tonic-gate sp->st_value = end; 2150Sstevel@tonic-gate /* 2160Sstevel@tonic-gate * Squirrel it away for later. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate if (bootaux[BA_BSS].ba_val == 0) 2190Sstevel@tonic-gate bootaux[BA_BSS].ba_val = end; 2200Sstevel@tonic-gate end += sp->st_size; 2210Sstevel@tonic-gate continue; 2220Sstevel@tonic-gate } else if (sp->st_shndx > (Half)sh_num) { 2230Sstevel@tonic-gate BSVC_PUTCHAR(syscallp, '>'); 2240Sstevel@tonic-gate return; 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * Symbol's new address. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate sp->st_value += section[sp->st_shndx]->sh_addr; 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * Allocate bss for COMMON, if any. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate if (end > edata) { 2380Sstevel@tonic-gate unsigned long va, bva; 2390Sstevel@tonic-gate unsigned long asize; 2400Sstevel@tonic-gate unsigned long align; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate if (bootaux[BA_LPAGESZ].ba_val) { 2430Sstevel@tonic-gate asize = bootaux[BA_LPAGESZ].ba_val; 2440Sstevel@tonic-gate align = bootaux[BA_LPAGESZ].ba_val; 2450Sstevel@tonic-gate } else { 2460Sstevel@tonic-gate asize = bootaux[BA_PAGESZ].ba_val; 2470Sstevel@tonic-gate align = BO_NO_ALIGN; 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate va = roundup(edata, asize); 2500Sstevel@tonic-gate bva = roundup(end, asize); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if (bva > va) { 2530Sstevel@tonic-gate bva = (unsigned long)BOP_ALLOC(bop, (caddr_t)va, 2540Sstevel@tonic-gate bva - va, align); 2550Sstevel@tonic-gate if (bva == NULL) 2560Sstevel@tonic-gate return; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Zero it. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate for (va = edata; va < end; va++) 2620Sstevel@tonic-gate *(char *)va = 0; 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Update the size of data. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate phdr->p_memsz += (end - edata); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * Relocate our own symbols. We'll handle the 2710Sstevel@tonic-gate * undefined symbols later. 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate for (i = 1; i < sh_num; i++) { 2740Sstevel@tonic-gate Shdr *rshp, *shp, *ssp; 2750Sstevel@tonic-gate unsigned long baseaddr, reladdr, rend; 2760Sstevel@tonic-gate int relocsize; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate rshp = section[i]; 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate if (rshp->sh_type != SHT_REL) 2810Sstevel@tonic-gate continue; 2820Sstevel@tonic-gate /* 2830Sstevel@tonic-gate * Get the section being relocated 2840Sstevel@tonic-gate * and the symbol table. 2850Sstevel@tonic-gate */ 2860Sstevel@tonic-gate shp = section[rshp->sh_info]; 2870Sstevel@tonic-gate ssp = section[rshp->sh_link]; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate reladdr = rshp->sh_addr; 2900Sstevel@tonic-gate baseaddr = shp->sh_addr; 2910Sstevel@tonic-gate rend = reladdr + rshp->sh_size; 2920Sstevel@tonic-gate relocsize = rshp->sh_entsize; 2930Sstevel@tonic-gate /* 2940Sstevel@tonic-gate * Loop through relocations. 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate while (reladdr < rend) { 2970Sstevel@tonic-gate Sym *symref; 2980Sstevel@tonic-gate Rel *reloc; 2990Sstevel@tonic-gate unsigned long stndx; 3000Sstevel@tonic-gate unsigned long off, *offptr; 3010Sstevel@tonic-gate long value; 3020Sstevel@tonic-gate int rtype; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate reloc = (Rel *)reladdr; 3050Sstevel@tonic-gate off = reloc->r_offset; 3060Sstevel@tonic-gate rtype = ELF32_R_TYPE(reloc->r_info); 3070Sstevel@tonic-gate stndx = ELF32_R_SYM(reloc->r_info); 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate reladdr += relocsize; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate if (rtype == R_386_NONE) { 3120Sstevel@tonic-gate continue; 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate off += baseaddr; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate if (rtype == R_386_RELATIVE) { 3170Sstevel@tonic-gate /* 3180Sstevel@tonic-gate * add base addr to reloc location 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate value = baseaddr; 3210Sstevel@tonic-gate } else { 3220Sstevel@tonic-gate unsigned int symoff, symsize; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate symsize = ssp->sh_entsize; 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate for (symoff = 0; stndx; stndx--) 3270Sstevel@tonic-gate symoff += symsize; 3280Sstevel@tonic-gate symref = (Sym *)(ssp->sh_addr + symoff); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* 3310Sstevel@tonic-gate * Check for bad symbol index. 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate if (symoff > ssp->sh_size) 3340Sstevel@tonic-gate return; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * Just bind our own symbols at this point. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate if (symref->st_shndx == SHN_UNDEF) { 3400Sstevel@tonic-gate continue; 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate value = symref->st_value; 3440Sstevel@tonic-gate if (ELF32_ST_BIND(symref->st_info) != 3450Sstevel@tonic-gate STB_LOCAL) { 3460Sstevel@tonic-gate /* 3470Sstevel@tonic-gate * If PC-relative, subtract ref addr. 3480Sstevel@tonic-gate */ 3490Sstevel@tonic-gate if (rtype == R_386_PC32 || 3500Sstevel@tonic-gate rtype == R_386_PLT32 || 3510Sstevel@tonic-gate rtype == R_386_GOTPC) 3520Sstevel@tonic-gate value -= off; 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate offptr = (unsigned long *)off; 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * insert value calculated at reference point 3580Sstevel@tonic-gate * 2 cases - normal byte order aligned, normal byte 3590Sstevel@tonic-gate * order unaligned. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate switch (rtype) { 3620Sstevel@tonic-gate case R_386_PC32: 3630Sstevel@tonic-gate case R_386_32: 3640Sstevel@tonic-gate case R_386_PLT32: 3650Sstevel@tonic-gate case R_386_RELATIVE: 3660Sstevel@tonic-gate *offptr += value; 3670Sstevel@tonic-gate break; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * For now, ignore GOT references ... 3710Sstevel@tonic-gate */ 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate case R_386_GOTPC: 3740Sstevel@tonic-gate #if defined(DEBUG) 3750Sstevel@tonic-gate BSVC_PUTCHAR(syscallp, 'p'); 3760Sstevel@tonic-gate #endif 3770Sstevel@tonic-gate break; 3780Sstevel@tonic-gate case R_386_GOTOFF: 3790Sstevel@tonic-gate BSVC_PUTCHAR(syscallp, 'g'); 3800Sstevel@tonic-gate break; 3810Sstevel@tonic-gate default: 3820Sstevel@tonic-gate BSVC_PUTCHAR(syscallp, 'r'); 3830Sstevel@tonic-gate return; 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * We only need to do it once. 3870Sstevel@tonic-gate */ 3880Sstevel@tonic-gate reloc->r_info = ELF32_R_INFO(stndx, R_386_NONE); 3890Sstevel@tonic-gate } /* while */ 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * Done relocating all of our *defined* 3940Sstevel@tonic-gate * symbols, so we hand off. 3950Sstevel@tonic-gate */ 3960Sstevel@tonic-gate kobj_init(syscallp, dvec, bootops, bootaux); 3970Sstevel@tonic-gate } 398