1179187Sjb/*-
2179187Sjb * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3179187Sjb * All rights reserved.
4179187Sjb *
5179187Sjb * Redistribution and use in source and binary forms, with or without
6179187Sjb * modification, are permitted provided that the following conditions
7179187Sjb * are met:
8179187Sjb * 1. Redistributions of source code must retain the above copyright
9179187Sjb *    notice, this list of conditions and the following disclaimer.
10179187Sjb * 2. Redistributions in binary form must reproduce the above copyright
11179187Sjb *    notice, this list of conditions and the following disclaimer in the
12179187Sjb *    documentation and/or other materials provided with the distribution.
13179187Sjb *
14179187Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15179187Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16179187Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17179187Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18179187Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19179187Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20179187Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21179187Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22179187Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23179187Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24179187Sjb * SUCH DAMAGE.
25179187Sjb *
26179187Sjb * $FreeBSD$
27179187Sjb */
28179187Sjb
29179187Sjb#include <stdlib.h>
30179187Sjb#include <string.h>
31179187Sjb#include "_libdwarf.h"
32179187Sjb
33179187Sjbint
34179187Sjbdwarf_attr(Dwarf_Die die, Dwarf_Half attr, Dwarf_Attribute *atp, Dwarf_Error *err)
35179187Sjb{
36179187Sjb	Dwarf_Attribute at;
37179187Sjb	Dwarf_Abbrev	a;
38179187Sjb	int ret = DWARF_E_NONE;
39179187Sjb
40179187Sjb	if (err == NULL)
41179187Sjb		return DWARF_E_ERROR;
42179187Sjb
43179187Sjb	if (die == NULL || atp == NULL || (a = die->die_a) == NULL) {
44179187Sjb		DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
45179187Sjb		return DWARF_E_ARGUMENT;
46179187Sjb	}
47179187Sjb
48179187Sjb	STAILQ_FOREACH(at, &a->a_attrib, at_next)
49179187Sjb		if (at->at_attrib == attr)
50179187Sjb			break;
51179187Sjb
52179187Sjb	*atp = at;
53179187Sjb
54179187Sjb	if (at == NULL) {
55179187Sjb		DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
56179187Sjb		ret = DWARF_E_NO_ENTRY;
57179187Sjb	}
58179187Sjb
59179187Sjb	return ret;
60179187Sjb}
61179187Sjb
62179187Sjbint
63179187Sjbdwarf_attr_add(Dwarf_Abbrev a, uint64_t attr, uint64_t form, Dwarf_Attribute *atp, Dwarf_Error *error)
64179187Sjb{
65179187Sjb	Dwarf_Attribute at;
66179187Sjb	int ret = DWARF_E_NONE;
67179187Sjb
68179187Sjb	if (error == NULL)
69179187Sjb		return DWARF_E_ERROR;
70179187Sjb
71179187Sjb	if (a == NULL) {
72179187Sjb		DWARF_SET_ERROR(error, DWARF_E_ARGUMENT);
73179187Sjb		return DWARF_E_ARGUMENT;
74179187Sjb	}
75179187Sjb
76179187Sjb	if ((at = malloc(sizeof(struct _Dwarf_Attribute))) == NULL) {
77179187Sjb		DWARF_SET_ERROR(error, DWARF_E_MEMORY);
78179187Sjb		return DWARF_E_MEMORY;
79179187Sjb	}
80179187Sjb
81179187Sjb	/* Initialise the attribute structure. */
82179187Sjb	at->at_attrib	= attr;
83179187Sjb	at->at_form	= form;
84179187Sjb
85179187Sjb	/* Add the attribute to the list in the abbrev. */
86179187Sjb	STAILQ_INSERT_TAIL(&a->a_attrib, at, at_next);
87179187Sjb
88179187Sjb	if (atp != NULL)
89179187Sjb		*atp = at;
90179187Sjb
91179187Sjb	return ret;
92179187Sjb}
93