xref: /illumos-gate/usr/src/uts/sparc/krtld/kobj_isa.c (revision 608eb926e14f4ba4736b2d59e891335f1cba9e1e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1994-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Miscellaneous ISA-specific code.
29  */
30 #include <sys/types.h>
31 #include <sys/elf.h>
32 #include <sys/kobj.h>
33 #include <sys/kobj_impl.h>
34 #include <sys/archsystm.h>
35 
36 extern	int	use_iflush;
37 
38 /*
39  * Check that an ELF header corresponds to this machine's
40  * instruction set architecture.  Used by kobj_load_module()
41  * to not get confused by a misplaced driver or kernel module
42  * built for a different ISA.
43  */
44 int
45 elf_mach_ok(Ehdr *h)
46 {
47 	/*
48 	 * XX64	This should eventually be folded into a v9-isa specific file
49 	 */
50 	return (h->e_ident[EI_DATA] == ELFDATA2MSB &&
51 	    (h->e_machine == EM_SPARCV9));
52 }
53 
54 
55 /*
56  * return non-zero for a bad-address
57  */
58 int
59 kobj_addrcheck(void *xmp, caddr_t adr)
60 {
61 	struct module *mp;
62 
63 	mp = (struct module *)xmp;
64 
65 	if ((adr >= mp->text && adr < mp->text + mp->text_size) ||
66 	    (adr >= mp->data && adr < mp->data + mp->data_size))
67 		return (0); /* ok */
68 	if (mp->bss && adr >= (caddr_t)mp->bss &&
69 	    adr < (caddr_t)mp->bss + mp->bss_size)
70 		return (0);
71 	return (1);
72 }
73 
74 /*
75  * Flush instruction cache after updating text
76  */
77 void
78 kobj_sync_instruction_memory(caddr_t addr, size_t len)
79 {
80 	caddr_t	eaddr = addr + len;
81 
82 	if (!use_iflush)
83 		return;
84 
85 	addr = (caddr_t)((uintptr_t)addr & -8l); /* sparc needs 8-byte align */
86 
87 	while (addr < eaddr) {
88 		doflush(addr);
89 		addr += 8;
90 	}
91 }
92 
93 /*
94  * Calculate memory image required for relocatable object.
95  */
96 /* ARGSUSED3 */
97 int
98 get_progbits_size(struct module *mp, struct proginfo *tp, struct proginfo *dp,
99     struct proginfo *sdp)
100 {
101 	struct proginfo *pp;
102 	uint_t shn;
103 	Shdr *shp;
104 
105 	/*
106 	 * loop through sections to find out how much space we need
107 	 * for text, data, (also bss that is already assigned)
108 	 */
109 	for (shn = 1; shn < mp->shnum; shn++) {
110 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
111 		if (!(shp->sh_flags & SHF_ALLOC))
112 			continue;
113 		if (shp->sh_addr != 0) {
114 			_kobj_printf(ops,
115 			    "%s non-zero sect addr in input file\n",
116 			    mp->filename);
117 			return (-1);
118 		}
119 		pp = (shp->sh_flags & SHF_WRITE)? dp : tp;
120 
121 		if (shp->sh_addralign > pp->align)
122 			pp->align = shp->sh_addralign;
123 		pp->size = ALIGN(pp->size, shp->sh_addralign);
124 		pp->size += ALIGN(shp->sh_size, 8);
125 	}
126 	tp->size = ALIGN(tp->size, 8);
127 	dp->size = ALIGN(dp->size, 8);
128 	return (0);
129 }
130