symbol-minimal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include "symbol.h"
  2. #include "util.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. long offset;
  107. if (need_swap) {
  108. phdr->p_type = bswap_32(phdr->p_type);
  109. phdr->p_offset = bswap_32(phdr->p_offset);
  110. phdr->p_filesz = bswap_32(phdr->p_filesz);
  111. }
  112. if (phdr->p_type != PT_NOTE)
  113. continue;
  114. buf_size = phdr->p_filesz;
  115. offset = phdr->p_offset;
  116. tmp = realloc(buf, buf_size);
  117. if (tmp == NULL)
  118. goto out_free;
  119. buf = tmp;
  120. fseek(fp, offset, SEEK_SET);
  121. if (fread(buf, buf_size, 1, fp) != 1)
  122. goto out_free;
  123. ret = read_build_id(buf, buf_size, bf, size, need_swap);
  124. if (ret == 0)
  125. ret = size;
  126. break;
  127. }
  128. } else {
  129. Elf64_Ehdr ehdr;
  130. Elf64_Phdr *phdr;
  131. if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
  132. goto out;
  133. if (need_swap) {
  134. ehdr.e_phoff = bswap_64(ehdr.e_phoff);
  135. ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
  136. ehdr.e_phnum = bswap_16(ehdr.e_phnum);
  137. }
  138. buf_size = ehdr.e_phentsize * ehdr.e_phnum;
  139. buf = malloc(buf_size);
  140. if (buf == NULL)
  141. goto out;
  142. fseek(fp, ehdr.e_phoff, SEEK_SET);
  143. if (fread(buf, buf_size, 1, fp) != 1)
  144. goto out_free;
  145. for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
  146. void *tmp;
  147. long offset;
  148. if (need_swap) {
  149. phdr->p_type = bswap_32(phdr->p_type);
  150. phdr->p_offset = bswap_64(phdr->p_offset);
  151. phdr->p_filesz = bswap_64(phdr->p_filesz);
  152. }
  153. if (phdr->p_type != PT_NOTE)
  154. continue;
  155. buf_size = phdr->p_filesz;
  156. offset = phdr->p_offset;
  157. tmp = realloc(buf, buf_size);
  158. if (tmp == NULL)
  159. goto out_free;
  160. buf = tmp;
  161. fseek(fp, offset, SEEK_SET);
  162. if (fread(buf, buf_size, 1, fp) != 1)
  163. goto out_free;
  164. ret = read_build_id(buf, buf_size, bf, size, need_swap);
  165. if (ret == 0)
  166. ret = size;
  167. break;
  168. }
  169. }
  170. out_free:
  171. free(buf);
  172. out:
  173. fclose(fp);
  174. return ret;
  175. }
  176. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  177. {
  178. int fd;
  179. int ret = -1;
  180. struct stat stbuf;
  181. size_t buf_size;
  182. void *buf;
  183. fd = open(filename, O_RDONLY);
  184. if (fd < 0)
  185. return -1;
  186. if (fstat(fd, &stbuf) < 0)
  187. goto out;
  188. buf_size = stbuf.st_size;
  189. buf = malloc(buf_size);
  190. if (buf == NULL)
  191. goto out;
  192. if (read(fd, buf, buf_size) != (ssize_t) buf_size)
  193. goto out_free;
  194. ret = read_build_id(buf, buf_size, build_id, size, false);
  195. out_free:
  196. free(buf);
  197. out:
  198. close(fd);
  199. return ret;
  200. }
  201. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  202. enum dso_binary_type type)
  203. {
  204. int fd = open(name, O_RDONLY);
  205. if (fd < 0)
  206. goto out_errno;
  207. ss->name = strdup(name);
  208. if (!ss->name)
  209. goto out_close;
  210. ss->fd = fd;
  211. ss->type = type;
  212. return 0;
  213. out_close:
  214. close(fd);
  215. out_errno:
  216. dso->load_errno = errno;
  217. return -1;
  218. }
  219. bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
  220. {
  221. /* Assume all sym sources could be a runtime image. */
  222. return true;
  223. }
  224. bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
  225. {
  226. return false;
  227. }
  228. void symsrc__destroy(struct symsrc *ss)
  229. {
  230. zfree(&ss->name);
  231. close(ss->fd);
  232. }
  233. int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
  234. struct symsrc *ss __maybe_unused,
  235. struct map *map __maybe_unused,
  236. symbol_filter_t filter __maybe_unused)
  237. {
  238. return 0;
  239. }
  240. static int fd__is_64_bit(int fd)
  241. {
  242. u8 e_ident[EI_NIDENT];
  243. if (lseek(fd, 0, SEEK_SET))
  244. return -1;
  245. if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
  246. return -1;
  247. if (memcmp(e_ident, ELFMAG, SELFMAG) ||
  248. e_ident[EI_VERSION] != EV_CURRENT)
  249. return -1;
  250. return e_ident[EI_CLASS] == ELFCLASS64;
  251. }
  252. enum dso_type dso__type_fd(int fd)
  253. {
  254. Elf64_Ehdr ehdr;
  255. int ret;
  256. ret = fd__is_64_bit(fd);
  257. if (ret < 0)
  258. return DSO__TYPE_UNKNOWN;
  259. if (ret)
  260. return DSO__TYPE_64BIT;
  261. if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
  262. return DSO__TYPE_UNKNOWN;
  263. if (ehdr.e_machine == EM_X86_64)
  264. return DSO__TYPE_X32BIT;
  265. return DSO__TYPE_32BIT;
  266. }
  267. int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
  268. struct symsrc *ss,
  269. struct symsrc *runtime_ss __maybe_unused,
  270. symbol_filter_t filter __maybe_unused,
  271. int kmodule __maybe_unused)
  272. {
  273. unsigned char build_id[BUILD_ID_SIZE];
  274. int ret;
  275. ret = fd__is_64_bit(ss->fd);
  276. if (ret >= 0)
  277. dso->is_64_bit = ret;
  278. if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
  279. dso__set_build_id(dso, build_id);
  280. }
  281. return 0;
  282. }
  283. int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
  284. mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
  285. bool *is_64_bit __maybe_unused)
  286. {
  287. return -1;
  288. }
  289. int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
  290. {
  291. return -1;
  292. }
  293. void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
  294. {
  295. }
  296. int kcore_copy(const char *from_dir __maybe_unused,
  297. const char *to_dir __maybe_unused)
  298. {
  299. return -1;
  300. }
  301. void symbol__elf_init(void)
  302. {
  303. }