xref: /illumos-gate/usr/src/lib/libdisasm/common/libdisasm.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <libdisasm.h>
30 #include <stdlib.h>
31 #ifdef DIS_STANDALONE
32 #include <mdb/mdb_modapi.h>
33 #endif
34 
35 static int _dis_errno;
36 
37 /*
38  * For the standalone library, we need to link against mdb's malloc/free.
39  * Otherwise, use the standard malloc/free.
40  */
41 #ifdef DIS_STANDALONE
42 void *
43 dis_zalloc(size_t bytes)
44 {
45 	return (mdb_zalloc(bytes, UM_SLEEP));
46 }
47 
48 void
49 dis_free(void *ptr, size_t bytes)
50 {
51 	mdb_free(ptr, bytes);
52 }
53 #else
54 void *
55 dis_zalloc(size_t bytes)
56 {
57 	return (calloc(1, bytes));
58 }
59 
60 /*ARGSUSED*/
61 void
62 dis_free(void *ptr, size_t bytes)
63 {
64 	free(ptr);
65 }
66 #endif
67 
68 int
69 dis_seterrno(int error)
70 {
71 	_dis_errno = error;
72 	return (-1);
73 }
74 
75 int
76 dis_errno(void)
77 {
78 	return (_dis_errno);
79 }
80 
81 const char *
82 dis_strerror(int error)
83 {
84 	switch (error) {
85 	case E_DIS_NOMEM:
86 		return ("out of memory");
87 	case E_DIS_INVALFLAG:
88 		return ("invalid flags for this architecture");
89 	default:
90 		return ("unknown error");
91 	}
92 }
93