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