symbol-minimal.c 7.2 KB

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