modpost.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <byteswap.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/mman.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <elf.h>
  14. #include "../../include/linux/module_symbol.h"
  15. #include <list_types.h>
  16. #include "elfconfig.h"
  17. /* On BSD-alike OSes elf.h defines these according to host's word size */
  18. #undef ELF_ST_BIND
  19. #undef ELF_ST_TYPE
  20. #undef ELF_R_SYM
  21. #undef ELF_R_TYPE
  22. #if KERNEL_ELFCLASS == ELFCLASS32
  23. #define Elf_Ehdr Elf32_Ehdr
  24. #define Elf_Shdr Elf32_Shdr
  25. #define Elf_Sym Elf32_Sym
  26. #define Elf_Addr Elf32_Addr
  27. #define Elf_Section Elf32_Half
  28. #define ELF_ST_BIND ELF32_ST_BIND
  29. #define ELF_ST_TYPE ELF32_ST_TYPE
  30. #define Elf_Rel Elf32_Rel
  31. #define Elf_Rela Elf32_Rela
  32. #define ELF_R_SYM ELF32_R_SYM
  33. #define ELF_R_TYPE ELF32_R_TYPE
  34. #else
  35. #define Elf_Ehdr Elf64_Ehdr
  36. #define Elf_Shdr Elf64_Shdr
  37. #define Elf_Sym Elf64_Sym
  38. #define Elf_Addr Elf64_Addr
  39. #define Elf_Section Elf64_Half
  40. #define ELF_ST_BIND ELF64_ST_BIND
  41. #define ELF_ST_TYPE ELF64_ST_TYPE
  42. #define Elf_Rel Elf64_Rel
  43. #define Elf_Rela Elf64_Rela
  44. #define ELF_R_SYM ELF64_R_SYM
  45. #define ELF_R_TYPE ELF64_R_TYPE
  46. #endif
  47. #define bswap(x) \
  48. ({ \
  49. _Static_assert(sizeof(x) == 1 || sizeof(x) == 2 || \
  50. sizeof(x) == 4 || sizeof(x) == 8, "bug"); \
  51. (typeof(x))(sizeof(x) == 2 ? bswap_16(x) : \
  52. sizeof(x) == 4 ? bswap_32(x) : \
  53. sizeof(x) == 8 ? bswap_64(x) : \
  54. x); \
  55. })
  56. #define TO_NATIVE(x) \
  57. (target_is_big_endian == host_is_big_endian ? x : bswap(x))
  58. #define __get_unaligned_t(type, ptr) ({ \
  59. const struct { type x; } __attribute__((__packed__)) *__pptr = \
  60. (typeof(__pptr))(ptr); \
  61. __pptr->x; \
  62. })
  63. #define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr))
  64. #define get_unaligned_native(ptr) \
  65. ({ \
  66. typeof(*(ptr)) _val = get_unaligned(ptr); \
  67. TO_NATIVE(_val); \
  68. })
  69. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  70. #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
  71. struct buffer {
  72. char *p;
  73. int pos;
  74. int size;
  75. };
  76. void __attribute__((format(printf, 2, 3)))
  77. buf_printf(struct buffer *buf, const char *fmt, ...);
  78. void
  79. buf_write(struct buffer *buf, const char *s, int len);
  80. /**
  81. * struct module_alias - auto-generated MODULE_ALIAS()
  82. *
  83. * @node: linked to module::aliases
  84. * @modname: name of the builtin module (only for vmlinux)
  85. * @str: a string for MODULE_ALIAS()
  86. */
  87. struct module_alias {
  88. struct list_head node;
  89. char *builtin_modname;
  90. char str[];
  91. };
  92. /**
  93. * struct module - represent a module (vmlinux or *.ko)
  94. *
  95. * @dump_file: path to the .symvers file if loaded from a file
  96. * @aliases: list head for module_aliases
  97. * @no_trim_symbol: .no_trim_symbol section data
  98. * @no_trim_symbol_len: length of the .no_trim_symbol section
  99. */
  100. struct module {
  101. struct list_head list;
  102. struct list_head exported_symbols;
  103. struct list_head unresolved_symbols;
  104. const char *dump_file;
  105. bool is_gpl_compatible;
  106. bool is_vmlinux;
  107. bool seen;
  108. bool has_init;
  109. bool has_cleanup;
  110. char srcversion[25];
  111. // Missing namespace dependencies
  112. struct list_head missing_namespaces;
  113. // Actual imported namespaces
  114. struct list_head imported_namespaces;
  115. struct list_head aliases;
  116. char *no_trim_symbol;
  117. unsigned int no_trim_symbol_len;
  118. char name[];
  119. };
  120. struct elf_info {
  121. size_t size;
  122. Elf_Ehdr *hdr;
  123. Elf_Shdr *sechdrs;
  124. Elf_Sym *symtab_start;
  125. Elf_Sym *symtab_stop;
  126. unsigned int export_symbol_secndx; /* .export_symbol section */
  127. char *strtab;
  128. char *modinfo;
  129. unsigned int modinfo_len;
  130. char *no_trim_symbol;
  131. unsigned int no_trim_symbol_len;
  132. /* support for 32bit section numbers */
  133. unsigned int num_sections; /* max_secindex + 1 */
  134. unsigned int secindex_strings;
  135. /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
  136. * take shndx from symtab_shndx_start[N] instead */
  137. Elf32_Word *symtab_shndx_start;
  138. Elf32_Word *symtab_shndx_stop;
  139. struct symsearch *symsearch;
  140. };
  141. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  142. static inline unsigned int get_secindex(const struct elf_info *info,
  143. const Elf_Sym *sym)
  144. {
  145. unsigned int index = sym->st_shndx;
  146. /*
  147. * Elf{32,64}_Sym::st_shndx is 2 byte. Big section numbers are available
  148. * in the .symtab_shndx section.
  149. */
  150. if (index == SHN_XINDEX)
  151. return info->symtab_shndx_start[sym - info->symtab_start];
  152. /*
  153. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  154. * the way to UINT_MAX-255..UINT_MAX, to avoid conflicting with real
  155. * section indices.
  156. */
  157. if (index >= SHN_LORESERVE && index <= SHN_HIRESERVE)
  158. return index - SHN_HIRESERVE - 1;
  159. return index;
  160. }
  161. /*
  162. * If there's no name there, ignore it; likewise, ignore it if it's
  163. * one of the magic symbols emitted used by current tools.
  164. *
  165. * Internal symbols created by tools should be ignored by modpost.
  166. */
  167. static inline bool is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  168. {
  169. const char *name = elf->strtab + sym->st_name;
  170. if (!name || !strlen(name))
  171. return false;
  172. return !is_mapping_symbol(name);
  173. }
  174. /* symsearch.c */
  175. void symsearch_init(struct elf_info *elf);
  176. void symsearch_finish(struct elf_info *elf);
  177. Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr,
  178. unsigned int secndx, bool allow_negative,
  179. Elf_Addr min_distance);
  180. /* file2alias.c */
  181. void handle_moddevtable(struct module *mod, struct elf_info *info,
  182. Elf_Sym *sym, const char *symname);
  183. /* sumversion.c */
  184. void get_src_version(const char *modname, char sum[], unsigned sumlen);
  185. /* from modpost.c */
  186. extern bool target_is_big_endian;
  187. extern bool host_is_big_endian;
  188. const char *get_basename(const char *path);
  189. char *read_text_file(const char *filename);
  190. char *get_line(char **stringp);
  191. void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
  192. void __attribute__((format(printf, 2, 3)))
  193. modpost_log(bool is_error, const char *fmt, ...);
  194. /*
  195. * warn - show the given message, then let modpost continue running, still
  196. * allowing modpost to exit successfully. This should be used when
  197. * we still allow to generate vmlinux and modules.
  198. *
  199. * error - show the given message, then let modpost continue running, but fail
  200. * in the end. This should be used when we should stop building vmlinux
  201. * or modules, but we can continue running modpost to catch as many
  202. * issues as possible.
  203. *
  204. * fatal - show the given message, and bail out immediately. This should be
  205. * used when there is no point to continue running modpost.
  206. */
  207. #define warn(fmt, args...) modpost_log(false, fmt, ##args)
  208. #define error(fmt, args...) modpost_log(true, fmt, ##args)
  209. #define fatal(fmt, args...) do { error(fmt, ##args); exit(1); } while (1)