xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/libdwarf_elf_access.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: libdwarf_elf_access.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2009 Kai Wang
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  *
16*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc  */
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libdwarf_elf_access.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libdwarf_elf_access.c 2070 2011-10-27 03:05:32Z jkoshy ");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc int
_dwarf_elf_get_section_info(void * obj,Dwarf_Half ndx,Dwarf_Obj_Access_Section * ret_section,int * error)35*0a6a1f1dSLionel Sambuc _dwarf_elf_get_section_info(void *obj, Dwarf_Half ndx,
36*0a6a1f1dSLionel Sambuc     Dwarf_Obj_Access_Section *ret_section, int *error)
37*0a6a1f1dSLionel Sambuc {
38*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
39*0a6a1f1dSLionel Sambuc 	GElf_Shdr *sh;
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc 	e = obj;
42*0a6a1f1dSLionel Sambuc 	assert(e != NULL);
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc 	if (ret_section == NULL) {
45*0a6a1f1dSLionel Sambuc 		if (error)
46*0a6a1f1dSLionel Sambuc 			*error = DW_DLE_ARGUMENT;
47*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
48*0a6a1f1dSLionel Sambuc 	}
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc 	if (ndx >= e->eo_seccnt) {
51*0a6a1f1dSLionel Sambuc 		if (error)
52*0a6a1f1dSLionel Sambuc 			*error = DW_DLE_NO_ENTRY;
53*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NO_ENTRY);
54*0a6a1f1dSLionel Sambuc 	}
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc 	sh = &e->eo_shdr[ndx];
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc 	ret_section->addr = sh->sh_addr;
59*0a6a1f1dSLionel Sambuc 	ret_section->size = sh->sh_size;
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc 	ret_section->name = elf_strptr(e->eo_elf, e->eo_strndx, sh->sh_name);
62*0a6a1f1dSLionel Sambuc 	if (ret_section->name == NULL) {
63*0a6a1f1dSLionel Sambuc 		if (error)
64*0a6a1f1dSLionel Sambuc 			*error = DW_DLE_ELF;
65*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
66*0a6a1f1dSLionel Sambuc 	}
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
69*0a6a1f1dSLionel Sambuc }
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc Dwarf_Endianness
_dwarf_elf_get_byte_order(void * obj)72*0a6a1f1dSLionel Sambuc _dwarf_elf_get_byte_order(void *obj)
73*0a6a1f1dSLionel Sambuc {
74*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc 	e = obj;
77*0a6a1f1dSLionel Sambuc 	assert(e != NULL);
78*0a6a1f1dSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc 	switch (e->eo_ehdr.e_ident[EI_DATA]) {
80*0a6a1f1dSLionel Sambuc 	case ELFDATA2MSB:
81*0a6a1f1dSLionel Sambuc 		return (DW_OBJECT_MSB);
82*0a6a1f1dSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc 	case ELFDATA2LSB:
84*0a6a1f1dSLionel Sambuc 	case ELFDATANONE:
85*0a6a1f1dSLionel Sambuc 	default:
86*0a6a1f1dSLionel Sambuc 		return (DW_OBJECT_LSB);
87*0a6a1f1dSLionel Sambuc 	}
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc Dwarf_Small
_dwarf_elf_get_length_size(void * obj)91*0a6a1f1dSLionel Sambuc _dwarf_elf_get_length_size(void *obj)
92*0a6a1f1dSLionel Sambuc {
93*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc 	e = obj;
96*0a6a1f1dSLionel Sambuc 	assert(e != NULL);
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc 	if (gelf_getclass(e->eo_elf) == ELFCLASS32)
99*0a6a1f1dSLionel Sambuc 		return (4);
100*0a6a1f1dSLionel Sambuc 	else if (e->eo_ehdr.e_machine == EM_MIPS)
101*0a6a1f1dSLionel Sambuc 		return (8);
102*0a6a1f1dSLionel Sambuc 	else
103*0a6a1f1dSLionel Sambuc 		return (4);
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc Dwarf_Small
_dwarf_elf_get_pointer_size(void * obj)107*0a6a1f1dSLionel Sambuc _dwarf_elf_get_pointer_size(void *obj)
108*0a6a1f1dSLionel Sambuc {
109*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
110*0a6a1f1dSLionel Sambuc 
111*0a6a1f1dSLionel Sambuc 	e = obj;
112*0a6a1f1dSLionel Sambuc 	assert(e != NULL);
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	if (gelf_getclass(e->eo_elf) == ELFCLASS32)
115*0a6a1f1dSLionel Sambuc 		return (4);
116*0a6a1f1dSLionel Sambuc 	else
117*0a6a1f1dSLionel Sambuc 		return (8);
118*0a6a1f1dSLionel Sambuc }
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
_dwarf_elf_get_section_count(void * obj)121*0a6a1f1dSLionel Sambuc _dwarf_elf_get_section_count(void *obj)
122*0a6a1f1dSLionel Sambuc {
123*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc 	e = obj;
126*0a6a1f1dSLionel Sambuc 	assert(e != NULL);
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc 	return (e->eo_seccnt);
129*0a6a1f1dSLionel Sambuc }
130*0a6a1f1dSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc int
_dwarf_elf_load_section(void * obj,Dwarf_Half ndx,Dwarf_Small ** ret_data,int * error)132*0a6a1f1dSLionel Sambuc _dwarf_elf_load_section(void *obj, Dwarf_Half ndx, Dwarf_Small** ret_data,
133*0a6a1f1dSLionel Sambuc     int *error)
134*0a6a1f1dSLionel Sambuc {
135*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
136*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Data *ed;
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc 	e = obj;
139*0a6a1f1dSLionel Sambuc 	assert(e != NULL);
140*0a6a1f1dSLionel Sambuc 
141*0a6a1f1dSLionel Sambuc 	if (ret_data == NULL) {
142*0a6a1f1dSLionel Sambuc 		if (error)
143*0a6a1f1dSLionel Sambuc 			*error = DW_DLE_ARGUMENT;
144*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
145*0a6a1f1dSLionel Sambuc 	}
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc 	if (ndx >= e->eo_seccnt) {
148*0a6a1f1dSLionel Sambuc 		if (error)
149*0a6a1f1dSLionel Sambuc 			*error = DW_DLE_NO_ENTRY;
150*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NO_ENTRY);
151*0a6a1f1dSLionel Sambuc 	}
152*0a6a1f1dSLionel Sambuc 
153*0a6a1f1dSLionel Sambuc 	ed = &e->eo_data[ndx];
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc 	if (ed->ed_alloc != NULL)
156*0a6a1f1dSLionel Sambuc 		*ret_data = ed->ed_alloc;
157*0a6a1f1dSLionel Sambuc 	else {
158*0a6a1f1dSLionel Sambuc 		if (ed->ed_data == NULL) {
159*0a6a1f1dSLionel Sambuc 			if (error)
160*0a6a1f1dSLionel Sambuc 				*error = DW_DLE_NO_ENTRY;
161*0a6a1f1dSLionel Sambuc 			return (DW_DLV_NO_ENTRY);
162*0a6a1f1dSLionel Sambuc 		}
163*0a6a1f1dSLionel Sambuc 		*ret_data = ed->ed_data->d_buf;
164*0a6a1f1dSLionel Sambuc 	}
165*0a6a1f1dSLionel Sambuc 
166*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
167*0a6a1f1dSLionel Sambuc }
168