symbol-minimal.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "symbol.h"
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <byteswap.h>
  6. #include <sys/stat.h>
  7. static bool check_need_swap(int file_endian)
  8. {
  9. const int data = 1;
  10. u8 *check = (u8 *)&data;
  11. int host_endian;
  12. if (check[0] == 1)
  13. host_endian = ELFDATA2LSB;
  14. else
  15. host_endian = ELFDATA2MSB;
  16. return host_endian != file_endian;
  17. }
  18. #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
  19. #define NT_GNU_BUILD_ID 3
  20. static int read_build_id(void *note_data, size_t note_len, void *bf,
  21. size_t size, bool need_swap)
  22. {
  23. struct {
  24. u32 n_namesz;
  25. u32 n_descsz;
  26. u32 n_type;
  27. } *nhdr;
  28. void *ptr;
  29. ptr = note_data;
  30. while (ptr < (note_data + note_len)) {
  31. const char *name;
  32. size_t namesz, descsz;
  33. nhdr = ptr;
  34. if (need_swap) {
  35. nhdr->n_namesz = bswap_32(nhdr->n_namesz);
  36. nhdr->n_descsz = bswap_32(nhdr->n_descsz);
  37. nhdr->n_type = bswap_32(nhdr->n_type);
  38. }
  39. namesz = NOTE_ALIGN(nhdr->n_namesz);
  40. descsz = NOTE_ALIGN(nhdr->n_descsz);
  41. ptr += sizeof(*nhdr);
  42. name = ptr;
  43. ptr += namesz;
  44. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  45. nhdr->n_namesz == sizeof("GNU")) {
  46. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  47. size_t sz = min(size, descsz);
  48. memcpy(bf, ptr, sz);
  49. memset(bf + sz, 0, size - sz);
  50. return 0;
  51. }
  52. }
  53. ptr += descsz;
  54. }
  55. return -1;
  56. }
  57. int filename__read_debuglink(const char *filename __maybe_unused,
  58. char *debuglink __maybe_unused,
  59. size_t size __maybe_unused)
  60. {
  61. return -1;
  62. }
  63. /*
  64. * Just try PT_NOTE header otherwise fails
  65. */
  66. int filename__read_build_id(const char *filename, void *bf, size_t size)
  67. {
  68. FILE *fp;
  69. int ret = -1;
  70. bool need_swap = false;
  71. u8 e_ident[EI_NIDENT];
  72. size_t buf_size;
  73. void *buf;
  74. int i;
  75. fp = fopen(filename, "r");
  76. if (fp == NULL)
  77. return -1;
  78. if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
  79. goto out;
  80. if (memcmp(e_ident, ELFMAG, SELFMAG) ||
  81. e_ident[EI_VERSION] != EV_CURRENT)
  82. goto out;
  83. need_swap = check_need_swap(e_ident[EI_DATA]);
  84. /* for simplicity */
  85. fseek(fp, 0, SEEK_SET);
  86. if (e_ident[EI_CLASS] == ELFCLASS32) {
  87. Elf32_Ehdr ehdr;
  88. Elf32_Phdr *phdr;
  89. if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
  90. goto out;
  91. if (need_swap) {
  92. ehdr.e_phoff = bswap_32(ehdr.e_phoff);
  93. ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
  94. ehdr.e_phnum = bswap_16(ehdr.e_phnum);
  95. }
  96. buf_size = ehdr.e_phentsize * ehdr.e_phnum;
  97. buf = malloc(buf_size);
  98. if (buf == NULL)
  99. goto out;
  100. fseek(fp, ehdr.e_phoff, SEEK_SET);
  101. if (fread(buf, buf_size, 1, fp) != 1)
  102. goto out_free;
  103. for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
  104. void *tmp;
  105. if (need_swap) {
  106. phdr->p_type = bswap_32(phdr->p_type);
  107. phdr->p_offset = bswap_32(phdr->p_offset);
  108. phdr->p_filesz = bswap_32(phdr->p_filesz);
  109. }
  110. if (phdr->p_type != PT_NOTE)
  111. continue;
  112. buf_size = phdr->p_filesz;
  113. tmp = realloc(buf, buf_size);
  114. if (tmp == NULL)
  115. goto out_free;
  116. buf = tmp;
  117. fseek(fp, phdr->p_offset, SEEK_SET);
  118. if (fread(buf, buf_size, 1, fp) != 1)
  119. goto out_free;
  120. ret = read_build_id(buf, buf_size, bf, size, need_swap);
  121. if (ret == 0)
  122. ret = size;
  123. break;
  124. }
  125. } else {
  126. Elf64_Ehdr ehdr;
  127. Elf64_Phdr *phdr;
  128. if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
  129. goto out;
  130. if (need_swap) {
  131. ehdr.e_phoff = bswap_64(ehdr.e_phoff);
  132. ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
  133. ehdr.e_phnum = bswap_16(ehdr.e_phnum);
  134. }
  135. buf_size = ehdr.e_phentsize * ehdr.e_phnum;
  136. buf = malloc(buf_size);
  137. if (buf == NULL)
  138. goto out;
  139. fseek(fp, ehdr.e_phoff, SEEK_SET);
  140. if (fread(buf, buf_size, 1, fp) != 1)
  141. goto out_free;
  142. for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
  143. void *tmp;
  144. if (need_swap) {
  145. phdr->p_type = bswap_32(phdr->p_type);
  146. phdr->p_offset = bswap_64(phdr->p_offset);
  147. phdr->p_filesz = bswap_64(phdr->p_filesz);
  148. }
  149. if (phdr->p_type != PT_NOTE)
  150. continue;
  151. buf_size = phdr->p_filesz;
  152. tmp = realloc(buf, buf_size);
  153. if (tmp == NULL)
  154. goto out_free;
  155. buf = tmp;
  156. fseek(fp, phdr->p_offset, SEEK_SET);
  157. if (fread(buf, buf_size, 1, fp) != 1)
  158. goto out_free;
  159. ret = read_build_id(buf, buf_size, bf, size, need_swap);
  160. if (ret == 0)
  161. ret = size;
  162. break;
  163. }
  164. }
  165. out_free:
  166. free(buf);
  167. out:
  168. fclose(fp);
  169. return ret;
  170. }
  171. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  172. {
  173. int fd;
  174. int ret = -1;
  175. struct stat stbuf;
  176. size_t buf_size;
  177. void *buf;
  178. fd = open(filename, O_RDONLY);
  179. if (fd < 0)
  180. return -1;
  181. if (fstat(fd, &stbuf) < 0)
  182. goto out;
  183. buf_size = stbuf.st_size;
  184. buf = malloc(buf_size);
  185. if (buf == NULL)
  186. goto out;
  187. if (read(fd, buf, buf_size) != (ssize_t) buf_size)
  188. goto out_free;
  189. ret = read_build_id(buf, buf_size, build_id, size, false);
  190. out_free:
  191. free(buf);
  192. out:
  193. close(fd);
  194. return ret;
  195. }
  196. int symsrc__init(struct symsrc *ss, struct dso *dso __maybe_unused,
  197. const char *name,
  198. enum dso_binary_type type)
  199. {
  200. int fd = open(name, O_RDONLY);
  201. if (fd < 0)
  202. return -1;
  203. ss->name = strdup(name);
  204. if (!ss->name)
  205. goto out_close;
  206. ss->type = type;
  207. return 0;
  208. out_close:
  209. close(fd);
  210. return -1;
  211. }
  212. bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
  213. {
  214. /* Assume all sym sources could be a runtime image. */
  215. return true;
  216. }
  217. bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
  218. {
  219. return false;
  220. }
  221. void symsrc__destroy(struct symsrc *ss)
  222. {
  223. free(ss->name);
  224. close(ss->fd);
  225. }
  226. int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
  227. struct symsrc *ss __maybe_unused,
  228. struct map *map __maybe_unused,
  229. symbol_filter_t filter __maybe_unused)
  230. {
  231. return 0;
  232. }
  233. int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
  234. struct symsrc *ss,
  235. struct symsrc *runtime_ss __maybe_unused,
  236. symbol_filter_t filter __maybe_unused,
  237. int kmodule __maybe_unused)
  238. {
  239. unsigned char *build_id[BUILD_ID_SIZE];
  240. if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
  241. dso__set_build_id(dso, build_id);
  242. return 1;
  243. }
  244. return 0;
  245. }
  246. int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
  247. mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
  248. bool *is_64_bit __maybe_unused)
  249. {
  250. return -1;
  251. }
  252. void symbol__elf_init(void)
  253. {
  254. }