/usr.bin/gprof/elf.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 144 lines · 89 code · 21 blank · 34 comment · 25 complexity · 8894d886a820500463d859a6f3a818dc MD5 · raw file

  1. /*-
  2. * Copyright (c) 1983, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #if 0
  30. /* From: */
  31. #ifndef lint
  32. static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
  33. #endif /* not lint */
  34. #endif
  35. #include <sys/cdefs.h>
  36. __FBSDID("$FreeBSD$");
  37. #include <sys/types.h>
  38. #include <sys/mman.h>
  39. #include <sys/stat.h>
  40. #include <machine/elf.h>
  41. #include <err.h>
  42. #include <fcntl.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include "gprof.h"
  46. static bool wantsym(const Elf_Sym *, const char *);
  47. /* Things which get -E excluded by default. */
  48. static char *excludes[] = { ".mcount", "_mcleanup", NULL };
  49. int
  50. elf_getnfile(const char *filename, char ***defaultEs)
  51. {
  52. int fd;
  53. Elf_Ehdr h;
  54. struct stat s;
  55. void *mapbase;
  56. const char *base;
  57. const Elf_Shdr *shdrs;
  58. const Elf_Shdr *sh_symtab;
  59. const Elf_Shdr *sh_strtab;
  60. const char *strtab;
  61. const Elf_Sym *symtab;
  62. int symtabct;
  63. int i;
  64. if ((fd = open(filename, O_RDONLY)) == -1)
  65. err(1, "%s", filename);
  66. if (read(fd, &h, sizeof h) != sizeof h || !IS_ELF(h)) {
  67. close(fd);
  68. return -1;
  69. }
  70. if (fstat(fd, &s) == -1)
  71. err(1, "cannot fstat %s", filename);
  72. if ((mapbase = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
  73. MAP_FAILED)
  74. err(1, "cannot mmap %s", filename);
  75. close(fd);
  76. base = (const char *)mapbase;
  77. shdrs = (const Elf_Shdr *)(base + h.e_shoff);
  78. /* Find the symbol table and associated string table section. */
  79. for (i = 1; i < h.e_shnum; i++)
  80. if (shdrs[i].sh_type == SHT_SYMTAB)
  81. break;
  82. if (i == h.e_shnum)
  83. errx(1, "%s has no symbol table", filename);
  84. sh_symtab = &shdrs[i];
  85. sh_strtab = &shdrs[sh_symtab->sh_link];
  86. symtab = (const Elf_Sym *)(base + sh_symtab->sh_offset);
  87. symtabct = sh_symtab->sh_size / sh_symtab->sh_entsize;
  88. strtab = (const char *)(base + sh_strtab->sh_offset);
  89. /* Count the symbols that we're interested in. */
  90. nname = 0;
  91. for (i = 1; i < symtabct; i++)
  92. if (wantsym(&symtab[i], strtab))
  93. nname++;
  94. /* Allocate memory for them, plus a terminating entry. */
  95. if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
  96. errx(1, "insufficient memory for symbol table");
  97. /* Read them in. */
  98. npe = nl;
  99. for (i = 1; i < symtabct; i++) {
  100. const Elf_Sym *sym = &symtab[i];
  101. if (wantsym(sym, strtab)) {
  102. npe->value = sym->st_value;
  103. npe->name = strtab + sym->st_name;
  104. npe++;
  105. }
  106. }
  107. npe->value = -1;
  108. *defaultEs = excludes;
  109. return 0;
  110. }
  111. static bool
  112. wantsym(const Elf_Sym *sym, const char *strtab)
  113. {
  114. int type;
  115. int bind;
  116. type = ELF_ST_TYPE(sym->st_info);
  117. bind = ELF_ST_BIND(sym->st_info);
  118. if (type != STT_FUNC ||
  119. (aflag && bind == STB_LOCAL) ||
  120. (uflag && strchr(strtab + sym->st_name, '.') != NULL))
  121. return 0;
  122. return 1;
  123. }