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