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
53898Srsb * Common Development and Distribution License (the "License").
63898Srsb * 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 /*
223898Srsb * Copyright 2007 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 #include <fs/fs_subr.h>
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/elf.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/file.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/kobj.h>
350Sstevel@tonic-gate #include <sys/objfs.h>
360Sstevel@tonic-gate #include <sys/objfs_impl.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
403898Srsb #include <sys/vfs_opreg.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * /system/object/<obj>/object
440Sstevel@tonic-gate *
450Sstevel@tonic-gate * This is an ELF file that contains information about data stored in the
460Sstevel@tonic-gate * kernel. We use a special ELF file type, ET_SUNWPSEUDO, so that we can
470Sstevel@tonic-gate * control which fields and sections have meaning. The file contains the
480Sstevel@tonic-gate * following sections:
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * .shstrtab Section header string table
510Sstevel@tonic-gate * .SUNW_ctf CTF data
520Sstevel@tonic-gate * .symtab Symbol table
530Sstevel@tonic-gate * .strtab String table
540Sstevel@tonic-gate * .text Text
550Sstevel@tonic-gate * .data Data
560Sstevel@tonic-gate * .bss BSS
570Sstevel@tonic-gate * .filename Filename of module
580Sstevel@tonic-gate * .info Private module info structure
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * The .text, .data, and .bss sections are all marked SHT_NOBITS, and the data
610Sstevel@tonic-gate * is not actually exported in the file for security reasons. The section
620Sstevel@tonic-gate * headers do contain the address and size of the sections, which is needed by
630Sstevel@tonic-gate * DTrace. The CTF data, symbol table, and string table are present only if
640Sstevel@tonic-gate * they exist in the kernel.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate
670Sstevel@tonic-gate typedef enum {
680Sstevel@tonic-gate SECT_TYPE_DATA,
690Sstevel@tonic-gate SECT_TYPE_SHSTRTAB,
700Sstevel@tonic-gate SECT_TYPE_DUMMY,
710Sstevel@tonic-gate SECT_TYPE_SYMTAB,
720Sstevel@tonic-gate SECT_TYPE_STRTAB,
730Sstevel@tonic-gate SECT_TYPE_FILENAME,
740Sstevel@tonic-gate SECT_TYPE_INFO
750Sstevel@tonic-gate } sect_type_t;
760Sstevel@tonic-gate
770Sstevel@tonic-gate typedef struct section_desc {
780Sstevel@tonic-gate sect_type_t sect_id;
790Sstevel@tonic-gate const char *sect_name;
800Sstevel@tonic-gate uintptr_t sect_addr;
810Sstevel@tonic-gate size_t sect_size;
820Sstevel@tonic-gate int sect_type;
830Sstevel@tonic-gate int sect_flags;
840Sstevel@tonic-gate size_t sect_str;
850Sstevel@tonic-gate int sect_link;
860Sstevel@tonic-gate int sect_entsize;
870Sstevel@tonic-gate int sect_align;
880Sstevel@tonic-gate } section_desc_t;
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * For data sections, 'addr' and 'size' refer to offsets within the module
920Sstevel@tonic-gate * structure where we can find the address and size of the section.
930Sstevel@tonic-gate */
940Sstevel@tonic-gate #define SECT_DATA(name, addr, size, type, flags, align) \
950Sstevel@tonic-gate { SECT_TYPE_DATA, name, offsetof(struct module, addr), \
960Sstevel@tonic-gate offsetof(struct module, size), type, flags, 0, 0, 0, align }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * The dummy section is the initial section of the file. It is put into this
1000Sstevel@tonic-gate * array only for convenience when reading the file.
1010Sstevel@tonic-gate */
1020Sstevel@tonic-gate #define SECT_DUMMY { SECT_TYPE_DUMMY, "", 0, 0, 0, 0, 0, 0, 0, 0 }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * The size of the symbol table and string table are not immediately available
1060Sstevel@tonic-gate * as an offset into the module struct, so we have to create individual types
1070Sstevel@tonic-gate * for each.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate #ifdef _LP64
1100Sstevel@tonic-gate #define SECT_SYMTAB(name, type, flags) \
1110Sstevel@tonic-gate { SECT_TYPE_SYMTAB, name, offsetof(struct module, symtbl), 0, type, \
1120Sstevel@tonic-gate flags, 0, 0, sizeof (Elf64_Sym), sizeof (uint64_t) }
1130Sstevel@tonic-gate #else
1140Sstevel@tonic-gate #define SECT_SYMTAB(name, type, flags) \
1150Sstevel@tonic-gate { SECT_TYPE_SYMTAB, name, offsetof(struct module, symtbl), 0, type, \
1160Sstevel@tonic-gate flags, 0, 0, sizeof (Elf32_Sym), sizeof (uint32_t) }
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate #define SECT_STRTAB(name, type, flags) \
1190Sstevel@tonic-gate { SECT_TYPE_STRTAB, name, offsetof(struct module, strings), 0, type, \
1200Sstevel@tonic-gate flags, 0, 0, 0, 1 }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate * The .shstrtab section is constructed when the module is first loaded.
1240Sstevel@tonic-gate */
1250Sstevel@tonic-gate #define SECT_SHSTRTAB(name, type, flags) \
1260Sstevel@tonic-gate { SECT_TYPE_SHSTRTAB, name, 0, 0, type, flags, 0, 0, 0, 1 }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * Generic module information (objfs_info_t)
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate #define SECT_INFO \
1320Sstevel@tonic-gate { SECT_TYPE_INFO, ".info", 0, 0, SHT_PROGBITS, 0, 0, 0, 0, \
1330Sstevel@tonic-gate sizeof (uint32_t) }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * Filename section.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate #define SECT_FILENAME \
1390Sstevel@tonic-gate { SECT_TYPE_FILENAME, ".filename", 0, 0, SHT_PROGBITS, 0, 0, 0, 0, 1 }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate static section_desc_t data_sections[] = {
1420Sstevel@tonic-gate SECT_DUMMY,
1430Sstevel@tonic-gate SECT_SHSTRTAB(".shstrtab",
1440Sstevel@tonic-gate SHT_STRTAB, SHF_STRINGS),
1450Sstevel@tonic-gate SECT_DATA(".SUNW_ctf", ctfdata, ctfsize,
1460Sstevel@tonic-gate SHT_PROGBITS, 0, sizeof (uint64_t)),
1470Sstevel@tonic-gate SECT_SYMTAB(".symtab", SHT_SYMTAB, 0),
1480Sstevel@tonic-gate SECT_STRTAB(".strtab", SHT_STRTAB, SHF_STRINGS),
1490Sstevel@tonic-gate SECT_DATA(".text", text, text_size,
1500Sstevel@tonic-gate SHT_NOBITS, SHF_ALLOC | SHF_EXECINSTR, 0),
1510Sstevel@tonic-gate SECT_DATA(".data", data, data_size,
1520Sstevel@tonic-gate SHT_NOBITS, SHF_WRITE | SHF_ALLOC, 0),
1530Sstevel@tonic-gate SECT_DATA(".bss", bss, bss_size,
1540Sstevel@tonic-gate SHT_NOBITS, SHF_WRITE | SHF_ALLOC, 0),
1550Sstevel@tonic-gate SECT_INFO,
1560Sstevel@tonic-gate SECT_FILENAME
1570Sstevel@tonic-gate };
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate #define NSECTIONS \
1600Sstevel@tonic-gate (sizeof (data_sections) / sizeof (section_desc_t))
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate #ifdef _LP64
1630Sstevel@tonic-gate #define SECTION_OFFSET(section) \
1640Sstevel@tonic-gate (sizeof (Elf64_Ehdr) + (section) * sizeof (Elf64_Shdr))
1650Sstevel@tonic-gate #else
1660Sstevel@tonic-gate #define SECTION_OFFSET(section) \
1670Sstevel@tonic-gate (sizeof (Elf32_Ehdr) + (section) * sizeof (Elf32_Shdr))
1680Sstevel@tonic-gate #endif
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * Given a data node, returns the struct module appropriately locked. If the
1720Sstevel@tonic-gate * object has been unloaded, or re-loaded since the file was first opened, this
1730Sstevel@tonic-gate * function will return NULL. If successful, the caller must call
1740Sstevel@tonic-gate * objfs_data_unlock().
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate struct module *
objfs_data_lock(vnode_t * vp)1770Sstevel@tonic-gate objfs_data_lock(vnode_t *vp)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate objfs_datanode_t *dnode = vp->v_data;
1800Sstevel@tonic-gate objfs_odirnode_t *odir = gfs_file_parent(vp)->v_data;
1810Sstevel@tonic-gate struct modctl *mp = odir->objfs_odir_modctl;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate (void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if (mp->mod_mp == NULL ||
1860Sstevel@tonic-gate dnode->objfs_data_gencount < mp->mod_gencount) {
1870Sstevel@tonic-gate mod_release_mod(mp);
1880Sstevel@tonic-gate return (NULL);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate return (mp->mod_mp);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate void
objfs_data_unlock(vnode_t * vp)1950Sstevel@tonic-gate objfs_data_unlock(vnode_t *vp)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate objfs_odirnode_t *odir = gfs_file_parent(vp)->v_data;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate mod_release_mod(odir->objfs_odir_modctl);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * Called when the filesystem is first loaded. Creates and initializes the
2050Sstevel@tonic-gate * section header string table, and fills in the sect_str members of the section
2060Sstevel@tonic-gate * descriptors. This information could be encoded at compile-time, but this
2070Sstevel@tonic-gate * way keeps the code more maintainable, as we don't have to worry about
2080Sstevel@tonic-gate * duplicating information.
2090Sstevel@tonic-gate */
2100Sstevel@tonic-gate void
objfs_data_init(void)2110Sstevel@tonic-gate objfs_data_init(void)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate int i, shstrtab, strtab, symtab;
2140Sstevel@tonic-gate size_t len = 0;
2150Sstevel@tonic-gate section_desc_t *sect;
2160Sstevel@tonic-gate char *strdata;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
2190Sstevel@tonic-gate sect = &data_sections[i];
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate ASSERT(sect->sect_align == 0 || ISP2(sect->sect_align));
2220Sstevel@tonic-gate ASSERT(sect->sect_align <= sizeof (uint64_t));
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate len += strlen(sect->sect_name) + 1;
2250Sstevel@tonic-gate if (strcmp(sect->sect_name, ".shstrtab") == 0)
2260Sstevel@tonic-gate shstrtab = i;
2270Sstevel@tonic-gate else if (strcmp(sect->sect_name, ".symtab") == 0)
2280Sstevel@tonic-gate symtab = i;
2290Sstevel@tonic-gate else if (strcmp(sect->sect_name, ".strtab") == 0)
2300Sstevel@tonic-gate strtab = i;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate strdata = kmem_zalloc(len, KM_SLEEP);
2340Sstevel@tonic-gate sect = &data_sections[shstrtab];
2350Sstevel@tonic-gate sect->sect_addr = (uintptr_t)strdata;
2360Sstevel@tonic-gate sect->sect_size = len;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate len = 0;
2390Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
2400Sstevel@tonic-gate sect = &data_sections[i];
2410Sstevel@tonic-gate sect->sect_str = len;
2420Sstevel@tonic-gate bcopy(sect->sect_name, strdata + len,
2430Sstevel@tonic-gate strlen(sect->sect_name) + 1);
2440Sstevel@tonic-gate len += strlen(sect->sect_name) + 1;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate if (strcmp(sect->sect_name, ".SUNW_ctf") == 0)
2470Sstevel@tonic-gate sect->sect_link = symtab;
2480Sstevel@tonic-gate else if (strcmp(sect->sect_name, ".symtab") == 0)
2490Sstevel@tonic-gate sect->sect_link = strtab;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * Given a section descriptor and module pointer, return the address of the
2550Sstevel@tonic-gate * data.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate static uintptr_t
sect_addr(section_desc_t * sp,struct module * mp)2580Sstevel@tonic-gate sect_addr(section_desc_t *sp, struct module *mp)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate uintptr_t addr;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate switch (sp->sect_id) {
2630Sstevel@tonic-gate case SECT_TYPE_DUMMY:
2640Sstevel@tonic-gate addr = 0;
2650Sstevel@tonic-gate break;
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate case SECT_TYPE_SHSTRTAB:
2680Sstevel@tonic-gate addr = sp->sect_addr;
2690Sstevel@tonic-gate break;
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate case SECT_TYPE_STRTAB:
2720Sstevel@tonic-gate case SECT_TYPE_SYMTAB:
2730Sstevel@tonic-gate case SECT_TYPE_DATA:
2740Sstevel@tonic-gate addr = *((uintptr_t *)((char *)mp + sp->sect_addr));
2750Sstevel@tonic-gate break;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate case SECT_TYPE_FILENAME:
2780Sstevel@tonic-gate addr = (uintptr_t)mp->filename;
2790Sstevel@tonic-gate break;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate case SECT_TYPE_INFO:
2820Sstevel@tonic-gate addr = 1; /* This can be anything nonzero */
2830Sstevel@tonic-gate break;
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate return (addr);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate * Given a section descriptor and module pointer, return the size of the data.
2910Sstevel@tonic-gate */
2920Sstevel@tonic-gate static size_t
sect_size(section_desc_t * sp,struct module * mp)2930Sstevel@tonic-gate sect_size(section_desc_t *sp, struct module *mp)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate size_t size;
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate switch (sp->sect_id) {
2980Sstevel@tonic-gate case SECT_TYPE_DUMMY:
2990Sstevel@tonic-gate size = 0;
3000Sstevel@tonic-gate break;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate case SECT_TYPE_SHSTRTAB:
3030Sstevel@tonic-gate size = sp->sect_size;
3040Sstevel@tonic-gate break;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate case SECT_TYPE_DATA:
3070Sstevel@tonic-gate size = *((size_t *)((char *)mp + sp->sect_size));
3080Sstevel@tonic-gate break;
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate case SECT_TYPE_SYMTAB:
3110Sstevel@tonic-gate size = mp->symhdr->sh_size;
3120Sstevel@tonic-gate break;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate case SECT_TYPE_STRTAB:
3150Sstevel@tonic-gate size = mp->strhdr->sh_size;
3160Sstevel@tonic-gate break;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate case SECT_TYPE_INFO:
3190Sstevel@tonic-gate size = sizeof (objfs_info_t);
3200Sstevel@tonic-gate break;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate case SECT_TYPE_FILENAME:
3230Sstevel@tonic-gate if (mp->filename == NULL)
3240Sstevel@tonic-gate size = 0;
3250Sstevel@tonic-gate else
3260Sstevel@tonic-gate size = strlen(mp->filename) + 1;
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate return (size);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate * Given a section descriptor and module pointer, return 1 if the section has
3340Sstevel@tonic-gate * valid data and should be included, 0 otherwise.
3350Sstevel@tonic-gate */
3360Sstevel@tonic-gate static int
sect_valid(section_desc_t * sp,struct module * mp)3370Sstevel@tonic-gate sect_valid(section_desc_t *sp, struct module *mp)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate if (sp->sect_id == SECT_TYPE_DUMMY ||
3400Sstevel@tonic-gate sect_addr(sp, mp) != 0)
3410Sstevel@tonic-gate return (1);
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate return (0);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate * Given a section descriptor and module pointer, return the offset into the
3480Sstevel@tonic-gate * file where the data should be placed.
3490Sstevel@tonic-gate */
3500Sstevel@tonic-gate static size_t
data_offset(section_desc_t * sp,struct module * mp)3510Sstevel@tonic-gate data_offset(section_desc_t *sp, struct module *mp)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate int i;
3540Sstevel@tonic-gate size_t len;
3550Sstevel@tonic-gate section_desc_t *cp;
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate if (sp != NULL && mp != NULL && !sect_valid(sp, mp))
3580Sstevel@tonic-gate return (0);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate #ifdef _LP64
3610Sstevel@tonic-gate len = sizeof (Elf64_Ehdr);
3620Sstevel@tonic-gate #else
3630Sstevel@tonic-gate len = sizeof (Elf32_Ehdr);
3640Sstevel@tonic-gate #endif
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * Do a first pass to account for all the section headers.
3680Sstevel@tonic-gate */
3690Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
3700Sstevel@tonic-gate if (sect_valid(&data_sections[i], mp)) {
3710Sstevel@tonic-gate #ifdef _LP64
3720Sstevel@tonic-gate len += sizeof (Elf64_Shdr);
3730Sstevel@tonic-gate #else
3740Sstevel@tonic-gate len += sizeof (Elf32_Shdr);
3750Sstevel@tonic-gate #endif
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate * Add length of each section until we find the one we're looking for.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
3830Sstevel@tonic-gate cp = &data_sections[i];
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate * Align the section only if it's valid and contains data. When
3870Sstevel@tonic-gate * searching for a specific section, align the section before
3880Sstevel@tonic-gate * breaking out of the loop.
3890Sstevel@tonic-gate */
3900Sstevel@tonic-gate if (sect_valid(cp, mp) && cp->sect_type != SHT_NOBITS) {
3910Sstevel@tonic-gate if (cp->sect_align > 1)
3920Sstevel@tonic-gate len = P2ROUNDUP(len, cp->sect_align);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (sp != cp)
3950Sstevel@tonic-gate len += sect_size(cp, mp);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (sp == cp)
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate return (len);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate * Given an index into the section table and a module pointer, returns the
4070Sstevel@tonic-gate * data offset of the next section.
4080Sstevel@tonic-gate */
4090Sstevel@tonic-gate static size_t
next_offset(int idx,struct module * mp)4100Sstevel@tonic-gate next_offset(int idx, struct module *mp)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate int i;
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate for (i = idx + 1; i < NSECTIONS; i++) {
4150Sstevel@tonic-gate if (sect_valid(&data_sections[i], mp))
4160Sstevel@tonic-gate return (data_offset(&data_sections[i], mp));
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate return (data_offset(NULL, mp));
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate * Given a module pointer, return the total size needed for the file.
4240Sstevel@tonic-gate */
4250Sstevel@tonic-gate static size_t
data_size(struct module * mp)4260Sstevel@tonic-gate data_size(struct module *mp)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate return (data_offset(NULL, mp));
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate * Returns the size needed for all the headers in the file.
4330Sstevel@tonic-gate */
4340Sstevel@tonic-gate static size_t
header_size(void)4350Sstevel@tonic-gate header_size(void)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate return (data_offset(&data_sections[0], NULL));
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /* ARGSUSED */
4410Sstevel@tonic-gate vnode_t *
objfs_create_data(vnode_t * pvp)4420Sstevel@tonic-gate objfs_create_data(vnode_t *pvp)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate objfs_odirnode_t *onode = pvp->v_data;
4450Sstevel@tonic-gate vnode_t *vp = gfs_file_create(sizeof (objfs_datanode_t), pvp,
4460Sstevel@tonic-gate objfs_ops_data);
4470Sstevel@tonic-gate objfs_datanode_t *dnode = vp->v_data;
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate dnode->objfs_data_gencount = onode->objfs_odir_modctl->mod_gencount;
4500Sstevel@tonic-gate dnode->objfs_data_info.objfs_info_primary =
4510Sstevel@tonic-gate onode->objfs_odir_modctl->mod_prim;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate return (vp);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate /* ARGSUSED */
4570Sstevel@tonic-gate static int
objfs_data_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)458*5331Samw objfs_data_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
459*5331Samw caller_context_t *ct)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate struct module *mp;
4620Sstevel@tonic-gate timestruc_t now;
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate if ((mp = objfs_data_lock(vp)) == NULL)
4650Sstevel@tonic-gate return (EIO);
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate vap->va_type = VREG;
4680Sstevel@tonic-gate vap->va_mode = S_IRUSR | S_IRGRP | S_IROTH;
4690Sstevel@tonic-gate vap->va_nodeid = gfs_file_inode(vp);
4700Sstevel@tonic-gate vap->va_nlink = 1;
4710Sstevel@tonic-gate vap->va_size = data_size(mp);
4720Sstevel@tonic-gate gethrestime(&now);
4730Sstevel@tonic-gate vap->va_atime = vap->va_ctime = vap->va_mtime = now;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate (void) objfs_common_getattr(vp, vap);
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate objfs_data_unlock(vp);
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate return (0);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate /* ARGSUSED */
4830Sstevel@tonic-gate static int
objfs_data_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)484*5331Samw objfs_data_access(vnode_t *vp, int mode, int flags, cred_t *cr,
485*5331Samw caller_context_t *ct)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate if (mode & (VWRITE|VEXEC))
4880Sstevel@tonic-gate return (EACCES);
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate return (0);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate /* ARGSUSED */
4940Sstevel@tonic-gate int
objfs_data_open(vnode_t ** cpp,int flag,cred_t * cr,caller_context_t * ct)495*5331Samw objfs_data_open(vnode_t **cpp, int flag, cred_t *cr,
496*5331Samw caller_context_t *ct)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate if (flag & FWRITE)
4990Sstevel@tonic-gate return (EINVAL);
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate return (0);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate * Iterate over all symbols in the table and output each one individually,
5060Sstevel@tonic-gate * converting st_shndx to SHN_ABS for each symbol.
5070Sstevel@tonic-gate */
5080Sstevel@tonic-gate static int
read_symtab(void * addr,size_t size,off_t offset,uio_t * uio)5090Sstevel@tonic-gate read_symtab(void *addr, size_t size, off_t offset, uio_t *uio)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate #ifdef _LP64
5120Sstevel@tonic-gate Elf64_Sym sym, *symtab;
5130Sstevel@tonic-gate #else
5140Sstevel@tonic-gate Elf32_Sym sym, *symtab;
5150Sstevel@tonic-gate #endif
5160Sstevel@tonic-gate off_t index;
5170Sstevel@tonic-gate int error;
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate symtab = addr;
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate if (offset % sizeof (sym) != 0) {
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate * Be careful with the first symbol, as it is not
5240Sstevel@tonic-gate * symbol-aligned.
5250Sstevel@tonic-gate */
5260Sstevel@tonic-gate off_t partial = offset % sizeof (sym);
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate index = offset / sizeof (sym);
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate sym = symtab[index];
5310Sstevel@tonic-gate if (sym.st_shndx != SHN_UNDEF)
5320Sstevel@tonic-gate sym.st_shndx = SHN_ABS;
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate if ((error = uiomove((char *)&sym + partial,
5350Sstevel@tonic-gate sizeof (sym) - partial, UIO_READ, uio)) != 0 ||
5360Sstevel@tonic-gate uio->uio_resid <= 0)
5370Sstevel@tonic-gate return (error);
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate offset = (index + 1) * sizeof (sym);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate ASSERT(size % sizeof (sym) == 0);
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate for (index = offset / sizeof (sym); index < size / sizeof (sym);
5450Sstevel@tonic-gate index++) {
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate sym = symtab[index];
5480Sstevel@tonic-gate if (sym.st_shndx != SHN_UNDEF)
5490Sstevel@tonic-gate sym.st_shndx = SHN_ABS;
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate if ((error = uiomove((char *)&sym, sizeof (sym), UIO_READ,
5520Sstevel@tonic-gate uio)) != 0 || uio->uio_resid <= 0)
5530Sstevel@tonic-gate return (error);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate return (0);
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate /* ARGSUSED */
5600Sstevel@tonic-gate static int
objfs_data_read(vnode_t * vp,uio_t * uio,int ioflag,cred_t * cr,caller_context_t * ct)5610Sstevel@tonic-gate objfs_data_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr,
562*5331Samw caller_context_t *ct)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate int error = 0;
5650Sstevel@tonic-gate objfs_datanode_t *dnode = vp->v_data;
5660Sstevel@tonic-gate struct module *mp;
5670Sstevel@tonic-gate off_t off;
5680Sstevel@tonic-gate #ifdef _LP64
5690Sstevel@tonic-gate Elf64_Shdr shdr;
5700Sstevel@tonic-gate #else
5710Sstevel@tonic-gate Elf32_Shdr shdr;
5720Sstevel@tonic-gate #endif
5730Sstevel@tonic-gate int i, j;
5740Sstevel@tonic-gate section_desc_t *sp;
5750Sstevel@tonic-gate void *addr;
5760Sstevel@tonic-gate int transidx[NSECTIONS];
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate if ((mp = objfs_data_lock(vp)) == NULL)
5790Sstevel@tonic-gate return (ENOENT);
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate if (uio->uio_resid <= 0 || uio->uio_offset >= data_size(mp))
5820Sstevel@tonic-gate goto error;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate * Construct an array to translate from a generic section header index
5860Sstevel@tonic-gate * to an index specific for this object.
5870Sstevel@tonic-gate */
5880Sstevel@tonic-gate for (i = 0, j = 0; i < NSECTIONS; i++) {
5890Sstevel@tonic-gate transidx[i] = j;
5900Sstevel@tonic-gate if (sect_valid(&data_sections[i], mp))
5910Sstevel@tonic-gate j++;
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate * Check to see if we're in the Elf header
5970Sstevel@tonic-gate */
5980Sstevel@tonic-gate if (uio->uio_loffset < SECTION_OFFSET(0)) {
5990Sstevel@tonic-gate #ifdef _LP64
6000Sstevel@tonic-gate Elf64_Ehdr ehdr;
6010Sstevel@tonic-gate #else
6020Sstevel@tonic-gate Elf32_Ehdr ehdr;
6030Sstevel@tonic-gate #endif
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate bzero(&ehdr, sizeof (ehdr));
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate bcopy(ELFMAG, ehdr.e_ident, SELFMAG);
6080Sstevel@tonic-gate #ifdef _BIG_ENDIAN
6090Sstevel@tonic-gate ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
6100Sstevel@tonic-gate #else
6110Sstevel@tonic-gate ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
6120Sstevel@tonic-gate #endif
6130Sstevel@tonic-gate ehdr.e_ident[EI_VERSION] = EV_CURRENT;
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate #ifdef _LP64
6160Sstevel@tonic-gate ehdr.e_ident[EI_CLASS] = ELFCLASS64;
6170Sstevel@tonic-gate ehdr.e_type = ELFCLASS64;
6180Sstevel@tonic-gate ehdr.e_ehsize = sizeof (Elf64_Ehdr);
6190Sstevel@tonic-gate ehdr.e_phentsize = sizeof (Elf64_Phdr);
6200Sstevel@tonic-gate ehdr.e_shentsize = sizeof (Elf64_Shdr);
6210Sstevel@tonic-gate #else
6220Sstevel@tonic-gate ehdr.e_ident[EI_CLASS] = ELFCLASS32;
6230Sstevel@tonic-gate ehdr.e_type = ELFCLASS32;
6240Sstevel@tonic-gate ehdr.e_ehsize = sizeof (Elf32_Ehdr);
6250Sstevel@tonic-gate ehdr.e_phentsize = sizeof (Elf32_Phdr);
6260Sstevel@tonic-gate ehdr.e_shentsize = sizeof (Elf32_Shdr);
6270Sstevel@tonic-gate #endif
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate #ifdef __sparc
6300Sstevel@tonic-gate #ifdef __sparcv9
6310Sstevel@tonic-gate ehdr.e_machine = EM_SPARCV9;
6320Sstevel@tonic-gate #else
6330Sstevel@tonic-gate ehdr.e_machine = EM_SPARC;
6340Sstevel@tonic-gate #endif
6350Sstevel@tonic-gate #elif defined(__amd64)
6360Sstevel@tonic-gate ehdr.e_machine = EM_AMD64;
6370Sstevel@tonic-gate #else
6380Sstevel@tonic-gate ehdr.e_machine = EM_386;
6390Sstevel@tonic-gate #endif
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate ehdr.e_version = EV_CURRENT;
6420Sstevel@tonic-gate ehdr.e_type = ET_SUNWPSEUDO;
6430Sstevel@tonic-gate ehdr.e_shnum = 0;
6440Sstevel@tonic-gate ehdr.e_shoff = SECTION_OFFSET(0);
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
6470Sstevel@tonic-gate if (strcmp(data_sections[i].sect_name,
6480Sstevel@tonic-gate ".shstrtab") == 0)
6490Sstevel@tonic-gate ehdr.e_shstrndx = transidx[i];
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate if (sect_valid(&data_sections[i], mp))
6520Sstevel@tonic-gate ehdr.e_shnum++;
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate if ((error = uiomove((char *)&ehdr + uio->uio_loffset,
6560Sstevel@tonic-gate sizeof (ehdr) - uio->uio_loffset, UIO_READ, uio)) != 0 ||
6570Sstevel@tonic-gate uio->uio_resid <= 0)
6580Sstevel@tonic-gate goto error;
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate * Go through and construct section headers for each section.
6630Sstevel@tonic-gate */
6640Sstevel@tonic-gate j = 0;
6650Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
6660Sstevel@tonic-gate sp = &data_sections[i];
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate if (!sect_valid(sp, mp))
6690Sstevel@tonic-gate continue;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate if (uio->uio_loffset < SECTION_OFFSET(j+1)) {
6720Sstevel@tonic-gate shdr.sh_link = transidx[sp->sect_link];
6730Sstevel@tonic-gate shdr.sh_entsize = sp->sect_entsize;
6740Sstevel@tonic-gate shdr.sh_info = 0;
6750Sstevel@tonic-gate shdr.sh_name = sp->sect_str;
6760Sstevel@tonic-gate shdr.sh_type = sp->sect_type;
6770Sstevel@tonic-gate shdr.sh_flags = sp->sect_flags;
6780Sstevel@tonic-gate shdr.sh_addr = sect_addr(sp, mp);
6790Sstevel@tonic-gate shdr.sh_offset = data_offset(sp, mp);
6800Sstevel@tonic-gate shdr.sh_size = sect_size(sp, mp);
6810Sstevel@tonic-gate shdr.sh_addralign = sp->sect_align;
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate off = uio->uio_loffset - SECTION_OFFSET(j);
6840Sstevel@tonic-gate if ((error = uiomove((char *)&shdr + off,
6850Sstevel@tonic-gate sizeof (shdr) - off, UIO_READ, uio)) != 0 ||
6860Sstevel@tonic-gate uio->uio_resid <= 0)
6870Sstevel@tonic-gate goto error;
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate j++;
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate /*
6940Sstevel@tonic-gate * Output the data for each section
6950Sstevel@tonic-gate */
6960Sstevel@tonic-gate for (i = 0; i < NSECTIONS; i++) {
6970Sstevel@tonic-gate size_t nextoff;
6980Sstevel@tonic-gate sp = &data_sections[i];
6990Sstevel@tonic-gate nextoff = next_offset(i, mp);
7000Sstevel@tonic-gate if (sect_valid(sp, mp) && sp->sect_type != SHT_NOBITS &&
7010Sstevel@tonic-gate uio->uio_loffset < nextoff) {
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate if (sp->sect_id == SECT_TYPE_INFO)
7040Sstevel@tonic-gate addr = &dnode->objfs_data_info;
7050Sstevel@tonic-gate else
7060Sstevel@tonic-gate addr = (void *)sect_addr(sp, mp);
7070Sstevel@tonic-gate off = uio->uio_loffset - data_offset(sp, mp);
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate * The symtab requires special processing to convert
7110Sstevel@tonic-gate * the st_shndx field to SHN_ABS. Otherwise, simply
7120Sstevel@tonic-gate * copy the data in bulk.
7130Sstevel@tonic-gate */
7140Sstevel@tonic-gate if (sp->sect_id == SECT_TYPE_SYMTAB)
7150Sstevel@tonic-gate error = read_symtab(addr, sect_size(sp, mp),
7160Sstevel@tonic-gate off, uio);
7170Sstevel@tonic-gate else
7180Sstevel@tonic-gate error = uiomove((char *)addr + off,
7190Sstevel@tonic-gate sect_size(sp, mp) - off, UIO_READ, uio);
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate if (error != 0 || uio->uio_resid <= 0)
7220Sstevel@tonic-gate goto error;
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate * If the next section needs to be aligned, pad out with
7260Sstevel@tonic-gate * zeroes.
7270Sstevel@tonic-gate */
7280Sstevel@tonic-gate if (uio->uio_loffset < nextoff) {
7290Sstevel@tonic-gate uint64_t padding = 0;
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate ASSERT(nextoff - uio->uio_loffset <
7320Sstevel@tonic-gate sizeof (uint64_t));
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate if ((error = uiomove(&padding,
7350Sstevel@tonic-gate nextoff - uio->uio_loffset, UIO_READ,
7360Sstevel@tonic-gate uio)) != 0 || uio->uio_resid <= 0)
7370Sstevel@tonic-gate goto error;
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate error:
7440Sstevel@tonic-gate objfs_data_unlock(vp);
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate return (error);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate /* ARGSUSED */
7500Sstevel@tonic-gate static int
objfs_data_seek(vnode_t * vp,offset_t off,offset_t * offp,caller_context_t * ct)751*5331Samw objfs_data_seek(vnode_t *vp, offset_t off, offset_t *offp,
752*5331Samw caller_context_t *ct)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate return (0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate const fs_operation_def_t objfs_tops_data[] = {
7583898Srsb { VOPNAME_OPEN, { .vop_open = objfs_data_open } },
7593898Srsb { VOPNAME_CLOSE, { .vop_close = objfs_common_close } },
7603898Srsb { VOPNAME_IOCTL, { .error = fs_inval } },
7613898Srsb { VOPNAME_GETATTR, { .vop_getattr = objfs_data_getattr } },
7623898Srsb { VOPNAME_ACCESS, { .vop_access = objfs_data_access } },
7633898Srsb { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
7643898Srsb { VOPNAME_READ, { .vop_read = objfs_data_read } },
7653898Srsb { VOPNAME_SEEK, { .vop_seek = objfs_data_seek } },
7663898Srsb { VOPNAME_MAP, { .vop_map = gfs_vop_map } },
7670Sstevel@tonic-gate { NULL }
7680Sstevel@tonic-gate };
769