xref: /netbsd-src/external/bsd/elftoolchain/dist/libdwarf/dwarf_pro_macinfo.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: dwarf_pro_macinfo.c,v 1.5 2024/03/03 17:37:32 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2010 Kai Wang
59dd9d0cfSchristos  * All rights reserved.
69dd9d0cfSchristos  *
79dd9d0cfSchristos  * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos  * modification, are permitted provided that the following conditions
99dd9d0cfSchristos  * are met:
109dd9d0cfSchristos  * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos  *    notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos  *    documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos  *
169dd9d0cfSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos  * SUCH DAMAGE.
279dd9d0cfSchristos  */
289dd9d0cfSchristos 
299dd9d0cfSchristos #include "_libdwarf.h"
309dd9d0cfSchristos 
31*5ac3bc71Schristos __RCSID("$NetBSD: dwarf_pro_macinfo.c,v 1.5 2024/03/03 17:37:32 christos Exp $");
329dd9d0cfSchristos ELFTC_VCSID("Id: dwarf_pro_macinfo.c 2074 2011-10-27 03:34:33Z jkoshy");
339dd9d0cfSchristos 
349dd9d0cfSchristos static int
_dwarf_add_macro(Dwarf_P_Debug dbg,int type,Dwarf_Unsigned lineno,Dwarf_Signed fileindex,char * str1,char * str2,Dwarf_Error * error)359dd9d0cfSchristos _dwarf_add_macro(Dwarf_P_Debug dbg, int type, Dwarf_Unsigned lineno,
369dd9d0cfSchristos     Dwarf_Signed fileindex, char *str1, char *str2, Dwarf_Error *error)
379dd9d0cfSchristos {
389dd9d0cfSchristos 	Dwarf_Macro_Details *md;
399dd9d0cfSchristos 	int len;
409dd9d0cfSchristos 
419dd9d0cfSchristos 	dbg->dbgp_mdlist = realloc(dbg->dbgp_mdlist,
429dd9d0cfSchristos 	    (size_t) dbg->dbgp_mdcnt + 1);
439dd9d0cfSchristos 	if (dbg->dbgp_mdlist == NULL) {
449dd9d0cfSchristos 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
459dd9d0cfSchristos 		return (DW_DLV_ERROR);
469dd9d0cfSchristos 	}
479dd9d0cfSchristos 
489dd9d0cfSchristos 	md = &dbg->dbgp_mdlist[dbg->dbgp_mdcnt];
499dd9d0cfSchristos 	dbg->dbgp_mdcnt++;
509dd9d0cfSchristos 
519dd9d0cfSchristos 	md->dmd_offset = 0;
529dd9d0cfSchristos 	md->dmd_type = type;
539dd9d0cfSchristos 	md->dmd_lineno = lineno;
549dd9d0cfSchristos 	md->dmd_fileindex = fileindex;
559dd9d0cfSchristos 	md->dmd_macro = NULL;
569dd9d0cfSchristos 
579dd9d0cfSchristos 	if (str1 == NULL)
589dd9d0cfSchristos 		return (DW_DLV_OK);
599dd9d0cfSchristos 	else if (str2 == NULL) {
609dd9d0cfSchristos 		if ((md->dmd_macro = strdup(str1)) == NULL) {
619dd9d0cfSchristos 			dbg->dbgp_mdcnt--;
629dd9d0cfSchristos 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
639dd9d0cfSchristos 			return (DW_DLV_ERROR);
649dd9d0cfSchristos 		}
659dd9d0cfSchristos 		return (DW_DLV_OK);
669dd9d0cfSchristos 	} else {
679dd9d0cfSchristos 		len = strlen(str1) + strlen(str2) + 2;
689dd9d0cfSchristos 		if ((md->dmd_macro = malloc(len)) == NULL) {
699dd9d0cfSchristos 			dbg->dbgp_mdcnt--;
709dd9d0cfSchristos 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
719dd9d0cfSchristos 			return (DW_DLV_ERROR);
729dd9d0cfSchristos 		}
739dd9d0cfSchristos 		snprintf(md->dmd_macro, len, "%s %s", str1, str2);
749dd9d0cfSchristos 		return (DW_DLV_OK);
759dd9d0cfSchristos 	}
769dd9d0cfSchristos }
779dd9d0cfSchristos 
789dd9d0cfSchristos int
dwarf_def_macro(Dwarf_P_Debug dbg,Dwarf_Unsigned lineno,char * name,char * value,Dwarf_Error * error)799dd9d0cfSchristos dwarf_def_macro(Dwarf_P_Debug dbg, Dwarf_Unsigned lineno, char *name,
809dd9d0cfSchristos     char *value, Dwarf_Error *error)
819dd9d0cfSchristos {
829dd9d0cfSchristos 
839dd9d0cfSchristos 	if (dbg == NULL || name == NULL) {
849dd9d0cfSchristos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
859dd9d0cfSchristos 		return (DW_DLV_ERROR);
869dd9d0cfSchristos 	}
879dd9d0cfSchristos 
889dd9d0cfSchristos 	return (_dwarf_add_macro(dbg, DW_MACINFO_define, lineno, -1, name,
899dd9d0cfSchristos 	    value, error));
909dd9d0cfSchristos }
919dd9d0cfSchristos 
929dd9d0cfSchristos int
dwarf_undef_macro(Dwarf_P_Debug dbg,Dwarf_Unsigned lineno,char * name,Dwarf_Error * error)939dd9d0cfSchristos dwarf_undef_macro(Dwarf_P_Debug dbg, Dwarf_Unsigned lineno, char *name,
949dd9d0cfSchristos     Dwarf_Error *error)
959dd9d0cfSchristos {
969dd9d0cfSchristos 
979dd9d0cfSchristos 	if (dbg == NULL || name == NULL) {
989dd9d0cfSchristos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
999dd9d0cfSchristos 		return (DW_DLV_ERROR);
1009dd9d0cfSchristos 	}
1019dd9d0cfSchristos 
1029dd9d0cfSchristos 	return (_dwarf_add_macro(dbg, DW_MACINFO_undef, lineno, -1, name,
1039dd9d0cfSchristos 	    NULL, error));
1049dd9d0cfSchristos }
1059dd9d0cfSchristos 
1069dd9d0cfSchristos int
dwarf_start_macro_file(Dwarf_P_Debug dbg,Dwarf_Unsigned lineno,Dwarf_Unsigned fileindex,Dwarf_Error * error)1079dd9d0cfSchristos dwarf_start_macro_file(Dwarf_P_Debug dbg, Dwarf_Unsigned lineno,
1089dd9d0cfSchristos     Dwarf_Unsigned fileindex, Dwarf_Error *error)
1099dd9d0cfSchristos {
1109dd9d0cfSchristos 
1119dd9d0cfSchristos 	if (dbg == NULL) {
1129dd9d0cfSchristos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
1139dd9d0cfSchristos 		return (DW_DLV_ERROR);
1149dd9d0cfSchristos 	}
1159dd9d0cfSchristos 
1169dd9d0cfSchristos 	return (_dwarf_add_macro(dbg, DW_MACINFO_start_file, lineno, fileindex,
1179dd9d0cfSchristos 	    NULL, NULL, error));
1189dd9d0cfSchristos }
1199dd9d0cfSchristos 
1209dd9d0cfSchristos int
dwarf_end_macro_file(Dwarf_P_Debug dbg,Dwarf_Error * error)1219dd9d0cfSchristos dwarf_end_macro_file(Dwarf_P_Debug dbg, Dwarf_Error *error)
1229dd9d0cfSchristos {
1239dd9d0cfSchristos 
1249dd9d0cfSchristos 	if (dbg == NULL) {
1259dd9d0cfSchristos 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
1269dd9d0cfSchristos 		return (DW_DLV_ERROR);
1279dd9d0cfSchristos 	}
1289dd9d0cfSchristos 
1299dd9d0cfSchristos 	return (_dwarf_add_macro(dbg, DW_MACINFO_end_file, 0, -1,
1309dd9d0cfSchristos 	    NULL, NULL, error));
1319dd9d0cfSchristos }
1329dd9d0cfSchristos 
1339dd9d0cfSchristos int
dwarf_vendor_ext(Dwarf_P_Debug dbg,Dwarf_Unsigned constant,char * string,Dwarf_Error * error)1349dd9d0cfSchristos dwarf_vendor_ext(Dwarf_P_Debug dbg, Dwarf_Unsigned constant, char *string,
1359dd9d0cfSchristos     Dwarf_Error *error)
1369dd9d0cfSchristos {
1379dd9d0cfSchristos 
1389dd9d0cfSchristos 	if (dbg == NULL || string == NULL) {
1399dd9d0cfSchristos 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
1409dd9d0cfSchristos 		return (DW_DLV_ERROR);
1419dd9d0cfSchristos 	}
1429dd9d0cfSchristos 
1439dd9d0cfSchristos 	return (_dwarf_add_macro(dbg, DW_MACINFO_vendor_ext, constant, -1,
1449dd9d0cfSchristos 	    string, NULL, error));
1459dd9d0cfSchristos }
146