8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / prof / common / rdelf.c
blob79356b344dcab11be82652457032f641adc702b7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * ELF support routines for processing versioned mon.out files.
32 #include <stdlib.h>
33 #include <string.h>
34 #include "profv.h"
36 bool
37 is_shared_obj(char *name)
39 int fd;
40 Elf *elf;
41 GElf_Ehdr ehdr;
43 if ((fd = open(name, O_RDONLY)) == -1) {
44 (void) fprintf(stderr, "%s: can't open `%s'\n", cmdname, name);
45 exit(ERR_ELF);
48 if (elf_version(EV_CURRENT) == EV_NONE) {
49 (void) fprintf(stderr, "%s: libelf out of date\n", cmdname);
50 exit(ERR_ELF);
53 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
54 (void) fprintf(stderr, "%s: elf_begin failed\n", cmdname);
55 exit(ERR_ELF);
58 if (gelf_getehdr(elf, &ehdr) == NULL) {
59 (void) fprintf(stderr, "%s: can't read ELF header of %s\n",
60 cmdname, name);
61 exit(ERR_ELF);
64 (void) elf_end(elf);
65 (void) close(fd);
67 if (ehdr.e_type == ET_DYN)
68 return (TRUE);
69 else
70 return (FALSE);
73 static void
74 rm_dups(nltype *nl, size_t *nfuncs)
76 size_t i, prev = 0, ndx = 0;
77 int prev_type, prev_bind, cur_type;
79 for (i = 1; i < *nfuncs; i++) {
81 * If current value is different from prev, proceed.
83 if (nl[prev].value < nl[i].value) {
84 prev = i;
85 continue;
89 * If current and prev have the syminfo, rm the latter.
91 if (nl[prev].info == nl[i].info) {
92 nl[i].name = NULL;
93 continue;
96 prev_type = ELF_ST_TYPE(nl[prev].info);
97 prev_bind = ELF_ST_BIND(nl[prev].info);
98 cur_type = ELF_ST_TYPE(nl[i].info);
101 * Remove the one with STT_NOTYPE and keep the other.
103 if (prev_type != cur_type) {
104 if (prev_type != STT_NOTYPE)
105 nl[i].name = NULL;
106 else {
107 nl[prev].name = NULL;
108 prev = i;
110 continue;
114 * If they have the same type, take the stronger bound
115 * function
117 if (prev_bind != STB_WEAK)
118 nl[i].name = NULL;
119 else {
120 nl[prev].name = NULL;
121 prev = i;
127 * Actually remove the cleared symbols from namelist. We're not
128 * truncating namelist by realloc, though.
130 for (i = 0; (i < *nfuncs) && (nl[i].name != NULL); i++)
133 ndx = i;
134 for (i = ndx + 1; i < *nfuncs; i++) {
135 if (nl[i].name) {
136 nl[ndx] = nl[i];
137 ndx++;
141 *nfuncs = ndx;
145 cmp_by_address(const void *arg1, const void *arg2)
147 nltype *a = (nltype *)arg1;
148 nltype *b = (nltype *)arg2;
150 if (a->value < b->value)
151 return (-1);
152 else if (a->value > b->value)
153 return (1);
154 else
155 return (0);
158 static int
159 is_function(Elf *elf, GElf_Sym *sym)
161 Elf_Scn *scn;
162 GElf_Shdr shdr;
165 * With dynamic linking, it is possible that certain undefined
166 * symbols exist in the objects. The actual definition will be
167 * found elsewhere, so we'll just skip it for this object.
169 if (sym->st_shndx == SHN_UNDEF)
170 return (0);
172 if (GELF_ST_TYPE(sym->st_info) == STT_FUNC) {
173 if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL)
174 return (1);
176 if (GELF_ST_BIND(sym->st_info) == STB_WEAK)
177 return (1);
179 if (gflag && GELF_ST_BIND(sym->st_info) == STB_LOCAL)
180 return (1);
184 * It's not a function; determine if it's in an executable section.
186 if (GELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
187 return (0);
190 * If it isn't global, and it isn't weak, and it isn't
191 * a 'local with the gflag set', then get out.
193 if (GELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
194 GELF_ST_BIND(sym->st_info) != STB_WEAK &&
195 !(gflag && GELF_ST_BIND(sym->st_info) == STB_LOCAL))
196 return (0);
198 if (sym->st_shndx >= SHN_LORESERVE)
199 return (0);
201 scn = elf_getscn(elf, sym->st_shndx);
202 (void) gelf_getshdr(scn, &shdr);
204 if (!(shdr.sh_flags & SHF_EXECINSTR))
205 return (0);
207 return (1);
210 static void
211 fetch_symtab(Elf *elf, char *filename, mod_info_t *module)
213 Elf_Scn *scn = NULL, *sym_pri = NULL, *sym_aux = NULL;
214 GElf_Word strndx = 0;
215 size_t i, nsyms, nfuncs;
216 GElf_Xword nsyms_pri, nsyms_aux = 0;
217 Elf_Data *symdata_pri, *symdata_aux;
218 nltype *nl, *npe;
219 int symtab_found = 0;
223 * Scan the section headers looking for a symbol table. Our
224 * preference is to use .symtab, because it contains the full
225 * set of symbols. If we find it, we stop looking immediately
226 * and use it. In the absence of a .symtab section, we are
227 * willing to use the dynamic symbol table (.dynsym), possibly
228 * augmented by the .SUNW_ldynsym, which contains local symbols.
230 while ((symtab_found == 0) && ((scn = elf_nextscn(elf, scn)) != NULL)) {
232 GElf_Shdr shdr;
234 if (gelf_getshdr(scn, &shdr) == NULL)
235 continue;
237 switch (shdr.sh_type) {
238 case SHT_SYMTAB:
239 nsyms_pri = shdr.sh_size / shdr.sh_entsize;
240 strndx = shdr.sh_link;
241 sym_pri = scn;
242 /* Throw away .SUNW_ldynsym. It is for .dynsym only */
243 nsyms_aux = 0;
244 sym_aux = NULL;
245 /* We have found the best symbol table. Stop looking */
246 symtab_found = 1;
247 break;
249 case SHT_DYNSYM:
250 /* We will use .dynsym if no .symtab is found */
251 nsyms_pri = shdr.sh_size / shdr.sh_entsize;
252 strndx = shdr.sh_link;
253 sym_pri = scn;
254 break;
256 case SHT_SUNW_LDYNSYM:
257 /* Auxiliary table, used with .dynsym */
258 nsyms_aux = shdr.sh_size / shdr.sh_entsize;
259 sym_aux = scn;
260 break;
264 if (sym_pri == NULL || strndx == 0) {
265 (void) fprintf(stderr, "%s: missing symbol table in %s\n",
266 cmdname, filename);
267 exit(ERR_ELF);
270 nsyms = (size_t)(nsyms_pri + nsyms_aux);
271 if ((nsyms_pri + nsyms_aux) != (GElf_Xword)nsyms) {
272 (void) fprintf(stderr,
273 "%s: can't handle more than 2^32 symbols", cmdname);
274 exit(ERR_INPUT);
277 if ((symdata_pri = elf_getdata(sym_pri, NULL)) == NULL) {
278 (void) fprintf(stderr, "%s: can't read symbol data from %s\n",
279 cmdname, filename);
280 exit(ERR_ELF);
283 if ((sym_aux != NULL) &&
284 ((symdata_aux = elf_getdata(sym_aux, NULL)) == NULL)) {
285 (void) fprintf(stderr,
286 "%s: can't read .SUNW_ldynsym symbol data from %s\n",
287 cmdname, filename);
288 exit(ERR_ELF);
291 if ((npe = nl = (nltype *) calloc(nsyms, sizeof (nltype))) == NULL) {
292 (void) fprintf(stderr, "%s: can't alloc %x bytes for symbols\n",
293 cmdname, nsyms * sizeof (nltype));
294 exit(ERR_ELF);
298 * Now we need to cruise through the symbol table eliminating
299 * all non-functions from consideration, and making strings
300 * real.
302 nfuncs = 0;
304 for (i = 1; i < nsyms; i++) {
305 GElf_Sym gsym;
306 char *name;
309 * Look up the symbol. In the case where we have a
310 * .SUNW_ldynsym/.dynsym pair, we treat them as a single
311 * logical table, with the data in .SUNW_ldynsym coming
312 * before the data in .dynsym.
314 if (i >= nsyms_aux)
315 (void) gelf_getsym(symdata_pri, i - nsyms_aux, &gsym);
316 else
317 (void) gelf_getsym(symdata_aux, i, &gsym);
319 name = elf_strptr(elf, strndx, gsym.st_name);
322 * We're interested in this symbol if it's a function
324 if (is_function(elf, &gsym)) {
326 npe->name = name;
327 npe->value = gsym.st_value;
328 npe->size = gsym.st_size;
329 npe->info = gsym.st_info;
331 npe++;
332 nfuncs++;
335 if (strcmp(name, PRF_END) == 0)
336 module->data_end = gsym.st_value;
339 if (npe == nl) {
340 (void) fprintf(stderr, "%s: no valid functions in %s\n",
341 cmdname, filename);
342 exit(ERR_INPUT);
346 * And finally, sort the symbols by increasing address
347 * and remove the duplicates.
349 qsort(nl, nfuncs, sizeof (nltype), cmp_by_address);
350 rm_dups(nl, &nfuncs);
352 module->nl = nl;
353 module->nfuncs = nfuncs;
356 static GElf_Addr
357 get_txtorigin(Elf *elf, char *filename)
359 GElf_Ehdr ehdr;
360 GElf_Phdr phdr;
361 GElf_Half ndx;
362 GElf_Addr txt_origin = 0;
363 bool first_load_seg = TRUE;
365 if (gelf_getehdr(elf, &ehdr) == NULL) {
366 (void) fprintf(stderr, "%s: can't read ELF header of %s\n",
367 cmdname, filename);
368 exit(ERR_ELF);
371 for (ndx = 0; ndx < ehdr.e_phnum; ndx++) {
372 if (gelf_getphdr(elf, ndx, &phdr) == NULL)
373 continue;
375 if ((phdr.p_type == PT_LOAD) && !(phdr.p_flags & PF_W)) {
376 if (first_load_seg || phdr.p_vaddr < txt_origin)
377 txt_origin = phdr.p_vaddr;
379 if (first_load_seg)
380 first_load_seg = FALSE;
384 return (txt_origin);
387 void
388 get_syms(char *filename, mod_info_t *mi)
390 int fd;
391 Elf *elf;
393 if ((fd = open(filename, O_RDONLY)) == -1) {
394 perror(filename);
395 exit(ERR_SYSCALL);
398 if (elf_version(EV_CURRENT) == EV_NONE) {
399 (void) fprintf(stderr, "%s: libelf out of date\n", cmdname);
400 exit(ERR_ELF);
403 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
404 (void) fprintf(stderr, "%s: elf_begin failed\n", cmdname);
405 exit(ERR_ELF);
408 if (gelf_getclass(elf) != ELFCLASS64) {
409 (void) fprintf(stderr, "%s: unsupported mon.out format for "
410 "this class of object\n", cmdname);
411 exit(ERR_ELF);
414 mi->txt_origin = get_txtorigin(elf, filename);
416 fetch_symtab(elf, filename, mi);