2 * (C) Copyright 2007-2011 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
12 static Elf64_Shdr
*__get_section(int i
)
14 return ((void*)symtab
) + symtab
->e_shoff
+ symtab
->e_shentsize
* i
;
17 static void *get_section(int type
, int *entsize
, int *size
)
23 /* we want the string table associated with the symbol table */
24 if (type
== SHT_STRTAB
) {
29 for(i
=0; i
<symtab
->e_shnum
; i
++) {
30 cur
= __get_section(i
);
32 if (cur
->sh_type
== type
)
41 cur
= __get_section(cur
->sh_link
);
43 *entsize
= cur
->sh_entsize
;
45 return ((void*)symtab
) + cur
->sh_offset
;
48 void symtab_find_text_range(u64
*start
, u64
*end
)
58 tab
= get_section(SHT_SYMTAB
, &entsize
, &size
);
64 while(((void*)cur
)-((void*)tab
)<size
) {
65 cur
= ((void*)cur
) + entsize
;
67 bind
= cur
->st_info
>> 4;
68 type
= cur
->st_info
& 0xf;
70 if ((bind
!= STB_LOCAL
) && (bind
!= STB_GLOBAL
))
73 if ((type
!= STT_OBJECT
) && (type
!= STT_FUNC
))
76 if (s
> cur
->st_value
)
78 if (e
< (cur
->st_value
+ cur
->st_size
))
79 e
= cur
->st_value
+ cur
->st_size
;
86 char *symtab_lookup(u64 addr
, char *buf
, int buflen
)
94 tab
= get_section(SHT_SYMTAB
, &entsize
, &size
);
98 strtab
= get_section(SHT_STRTAB
, &dummy
, &strsize
);
104 while(((void*)cur
)-((void*)tab
)<size
) {
105 cur
= ((void*)cur
) + entsize
;
107 bind
= cur
->st_info
>> 4;
108 type
= cur
->st_info
& 0xf;
110 if ((bind
!= STB_LOCAL
) && (bind
!= STB_GLOBAL
))
113 if ((type
!= STT_OBJECT
) && (type
!= STT_FUNC
))
116 if (addr
< cur
->st_value
)
118 if (addr
>= (cur
->st_value
+ cur
->st_size
))
121 /* ok, found the symbol */
122 if (cur
->st_name
> strsize
)
125 snprintf(buf
, buflen
, "%s+%#llx",
126 strtab
+ cur
->st_name
, addr
- cur
->st_value
);