modpost.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  59. struct buffer {
  60. char *p;
  61. int pos;
  62. int size;
  63. };
  64. void __attribute__((format(printf, 2, 3)))
  65. buf_printf(struct buffer *buf, const char *fmt, ...);
  66. void
  67. buf_write(struct buffer *buf, const char *s, int len);
  68. struct module {
  69. struct list_head list;
  70. struct list_head exported_symbols;
  71. struct list_head unresolved_symbols;
  72. bool is_gpl_compatible;
  73. bool from_dump; /* true if module was loaded from *.symvers */
  74. bool is_vmlinux;
  75. bool seen;
  76. bool has_init;
  77. bool has_cleanup;
  78. struct buffer dev_table_buf;
  79. char srcversion[25];
  80. // Missing namespace dependencies
  81. struct list_head missing_namespaces;
  82. // Actual imported namespaces
  83. struct list_head imported_namespaces;
  84. char name[];
  85. };
  86. struct elf_info {
  87. size_t size;
  88. Elf_Ehdr *hdr;
  89. Elf_Shdr *sechdrs;
  90. Elf_Sym *symtab_start;
  91. Elf_Sym *symtab_stop;
  92. unsigned int export_symbol_secndx; /* .export_symbol section */
  93. char *strtab;
  94. char *modinfo;
  95. unsigned int modinfo_len;
  96. /* support for 32bit section numbers */
  97. unsigned int num_sections; /* max_secindex + 1 */
  98. unsigned int secindex_strings;
  99. /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
  100. * take shndx from symtab_shndx_start[N] instead */
  101. Elf32_Word *symtab_shndx_start;
  102. Elf32_Word *symtab_shndx_stop;
  103. struct symsearch *symsearch;
  104. };
  105. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  106. static inline unsigned int get_secindex(const struct elf_info *info,
  107. const Elf_Sym *sym)
  108. {
  109. unsigned int index = sym->st_shndx;
  110. /*
  111. * Elf{32,64}_Sym::st_shndx is 2 byte. Big section numbers are available
  112. * in the .symtab_shndx section.
  113. */
  114. if (index == SHN_XINDEX)
  115. return info->symtab_shndx_start[sym - info->symtab_start];
  116. /*
  117. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  118. * the way to UINT_MAX-255..UINT_MAX, to avoid conflicting with real
  119. * section indices.
  120. */
  121. if (index >= SHN_LORESERVE && index <= SHN_HIRESERVE)
  122. return index - SHN_HIRESERVE - 1;
  123. return index;
  124. }
  125. /*
  126. * If there's no name there, ignore it; likewise, ignore it if it's
  127. * one of the magic symbols emitted used by current tools.
  128. *
  129. * Internal symbols created by tools should be ignored by modpost.
  130. */
  131. static inline bool is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  132. {
  133. const char *name = elf->strtab + sym->st_name;
  134. if (!name || !strlen(name))
  135. return false;
  136. return !is_mapping_symbol(name);
  137. }
  138. /* symsearch.c */
  139. void symsearch_init(struct elf_info *elf);
  140. void symsearch_finish(struct elf_info *elf);
  141. Elf_Sym *symsearch_find_nearest(struct elf_info *elf, Elf_Addr addr,
  142. unsigned int secndx, bool allow_negative,
  143. Elf_Addr min_distance);
  144. /* file2alias.c */
  145. void handle_moddevtable(struct module *mod, struct elf_info *info,
  146. Elf_Sym *sym, const char *symname);
  147. void add_moddevtable(struct buffer *buf, struct module *mod);
  148. /* sumversion.c */
  149. void get_src_version(const char *modname, char sum[], unsigned sumlen);
  150. /* from modpost.c */
  151. extern bool target_is_big_endian;
  152. extern bool host_is_big_endian;
  153. char *read_text_file(const char *filename);
  154. char *get_line(char **stringp);
  155. void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
  156. void __attribute__((format(printf, 2, 3)))
  157. modpost_log(bool is_error, const char *fmt, ...);
  158. /*
  159. * warn - show the given message, then let modpost continue running, still
  160. * allowing modpost to exit successfully. This should be used when
  161. * we still allow to generate vmlinux and modules.
  162. *
  163. * error - show the given message, then let modpost continue running, but fail
  164. * in the end. This should be used when we should stop building vmlinux
  165. * or modules, but we can continue running modpost to catch as many
  166. * issues as possible.
  167. *
  168. * fatal - show the given message, and bail out immediately. This should be
  169. * used when there is no point to continue running modpost.
  170. */
  171. #define warn(fmt, args...) modpost_log(false, fmt, ##args)
  172. #define error(fmt, args...) modpost_log(true, fmt, ##args)
  173. #define fatal(fmt, args...) do { error(fmt, ##args); exit(1); } while (1)