xref: /illumos-gate/usr/src/uts/intel/amd64/krtld/kobj_isa.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 2004 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 
35 /*
36  * Check that an ELF header corresponds to this machine's
37  * instruction set architecture.  Used by kobj_load_module()
38  * to not get confused by a misplaced driver or kernel module
39  * built for a different ISA.
40  */
41 int
42 elf_mach_ok(Elf64_Ehdr *h)
43 {
44 	return ((h->e_ident[EI_DATA] == ELFDATA2LSB) &&
45 	    (h->e_machine == EM_AMD64));
46 }
47 
48 /*
49  * return non-zero for a bad address
50  */
51 int
52 kobj_addrcheck(void *xmp, caddr_t adr)
53 {
54 	struct module *mp;
55 
56 	mp = (struct module *)xmp;
57 
58 	if ((adr >= mp->text && adr < mp->text + mp->text_size) ||
59 	    (adr >= mp->data && adr < mp->data + mp->data_size))
60 		return (0); /* ok */
61 	if (mp->bss && adr >= (caddr_t)mp->bss &&
62 	    adr < (caddr_t)mp->bss + mp->bss_size)
63 		return (0);
64 	return (1);
65 }
66 
67 
68 /*
69  * Flush instruction cache after updating text
70  * 	This is a nop for this machine arch.
71  */
72 /*ARGSUSED*/
73 void
74 kobj_sync_instruction_memory(caddr_t addr, size_t len)
75 {}
76 
77 /*
78  * Calculate memory image required for relocable object.
79  */
80 /* ARGSUSED3 */
81 int
82 get_progbits_size(struct module *mp, struct proginfo *tp, struct proginfo *dp,
83 	struct proginfo *sdp)
84 {
85 	struct proginfo *pp;
86 	uint_t shn;
87 	Shdr *shp;
88 
89 	/*
90 	 * loop through sections to find out how much space we need
91 	 * for text, data, (also bss that is already assigned)
92 	 */
93 	for (shn = 1; shn < mp->hdr.e_shnum; shn++) {
94 		shp = (Shdr *)(mp->shdrs + shn * mp->hdr.e_shentsize);
95 		if (!(shp->sh_flags & SHF_ALLOC))
96 			continue;
97 		if (shp->sh_addr != 0) {
98 			_kobj_printf(ops,
99 			    "%s non-zero sect addr in input file\n",
100 			    mp->filename);
101 			return (-1);
102 		}
103 		pp = (shp->sh_flags & SHF_WRITE)? dp : tp;
104 
105 		if (shp->sh_addralign > pp->align)
106 			pp->align = shp->sh_addralign;
107 		pp->size = ALIGN(pp->size, shp->sh_addralign);
108 		pp->size += ALIGN(shp->sh_size, 8);
109 	}
110 	tp->size = ALIGN(tp->size, 8);
111 	dp->size = ALIGN(dp->size, 8);
112 	return (0);
113 }
114