symbol-minimal.c 5.9 KB

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