1 /*	$NetBSD: dwarf_pro_macinfo.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Kai Wang
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "_libdwarf.h"
30 
31 __RCSID("$NetBSD: dwarf_pro_macinfo.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32 ELFTC_VCSID("Id: dwarf_pro_macinfo.c 2074 2011-10-27 03:34:33Z jkoshy ");
33 
34 static int
_dwarf_add_macro(Dwarf_P_Debug dbg,int type,Dwarf_Unsigned lineno,Dwarf_Signed fileindex,char * str1,char * str2,Dwarf_Error * error)35 _dwarf_add_macro(Dwarf_P_Debug dbg, int type, Dwarf_Unsigned lineno,
36     Dwarf_Signed fileindex, char *str1, char *str2, Dwarf_Error *error)
37 {
38 	Dwarf_Macro_Details *md;
39 	int len;
40 
41 	dbg->dbgp_mdlist = realloc(dbg->dbgp_mdlist,
42 	    (size_t) dbg->dbgp_mdcnt + 1);
43 	if (dbg->dbgp_mdlist == NULL) {
44 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
45 		return (DW_DLV_ERROR);
46 	}
47 
48 	md = &dbg->dbgp_mdlist[dbg->dbgp_mdcnt];
49 	dbg->dbgp_mdcnt++;
50 
51 	md->dmd_offset = 0;
52 	md->dmd_type = type;
53 	md->dmd_lineno = lineno;
54 	md->dmd_fileindex = fileindex;
55 	md->dmd_macro = NULL;
56 
57 	if (str1 == NULL)
58 		return (DW_DLV_OK);
59 	else if (str2 == NULL) {
60 		if ((md->dmd_macro = strdup(str1)) == NULL) {
61 			dbg->dbgp_mdcnt--;
62 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
63 			return (DW_DLV_ERROR);
64 		}
65 		return (DW_DLV_OK);
66 	} else {
67 		len = strlen(str1) + strlen(str2) + 2;
68 		if ((md->dmd_macro = malloc(len)) == NULL) {
69 			dbg->dbgp_mdcnt--;
70 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
71 			return (DW_DLV_ERROR);
72 		}
73 		snprintf(md->dmd_macro, len, "%s %s", str1, str2);
74 		return (DW_DLV_OK);
75 	}
76 }
77 
78 int
dwarf_def_macro(Dwarf_P_Debug dbg,Dwarf_Unsigned lineno,char * name,char * value,Dwarf_Error * error)79 dwarf_def_macro(Dwarf_P_Debug dbg, Dwarf_Unsigned lineno, char *name,
80     char *value, Dwarf_Error *error)
81 {
82 
83 	if (dbg == NULL || name == NULL) {
84 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
85 		return (DW_DLV_ERROR);
86 	}
87 
88 	return (_dwarf_add_macro(dbg, DW_MACINFO_define, lineno, -1, name,
89 	    value, error));
90 }
91 
92 int
dwarf_undef_macro(Dwarf_P_Debug dbg,Dwarf_Unsigned lineno,char * name,Dwarf_Error * error)93 dwarf_undef_macro(Dwarf_P_Debug dbg, Dwarf_Unsigned lineno, char *name,
94     Dwarf_Error *error)
95 {
96 
97 	if (dbg == NULL || name == NULL) {
98 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
99 		return (DW_DLV_ERROR);
100 	}
101 
102 	return (_dwarf_add_macro(dbg, DW_MACINFO_undef, lineno, -1, name,
103 	    NULL, error));
104 }
105 
106 int
dwarf_start_macro_file(Dwarf_P_Debug dbg,Dwarf_Unsigned lineno,Dwarf_Unsigned fileindex,Dwarf_Error * error)107 dwarf_start_macro_file(Dwarf_P_Debug dbg, Dwarf_Unsigned lineno,
108     Dwarf_Unsigned fileindex, Dwarf_Error *error)
109 {
110 
111 	if (dbg == NULL) {
112 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
113 		return (DW_DLV_ERROR);
114 	}
115 
116 	return (_dwarf_add_macro(dbg, DW_MACINFO_start_file, lineno, fileindex,
117 	    NULL, NULL, error));
118 }
119 
120 int
dwarf_end_macro_file(Dwarf_P_Debug dbg,Dwarf_Error * error)121 dwarf_end_macro_file(Dwarf_P_Debug dbg, Dwarf_Error *error)
122 {
123 
124 	if (dbg == NULL) {
125 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
126 		return (DW_DLV_ERROR);
127 	}
128 
129 	return (_dwarf_add_macro(dbg, DW_MACINFO_end_file, 0, -1,
130 	    NULL, NULL, error));
131 }
132 
133 int
dwarf_vendor_ext(Dwarf_P_Debug dbg,Dwarf_Unsigned constant,char * string,Dwarf_Error * error)134 dwarf_vendor_ext(Dwarf_P_Debug dbg, Dwarf_Unsigned constant, char *string,
135     Dwarf_Error *error)
136 {
137 
138 	if (dbg == NULL || string == NULL) {
139 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
140 		return (DW_DLV_ERROR);
141 	}
142 
143 	return (_dwarf_add_macro(dbg, DW_MACINFO_vendor_ext, constant, -1,
144 	    string, NULL, error));
145 }
146