modpost.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/mman.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <elf.h>
  13. #include "list.h"
  14. #include "elfconfig.h"
  15. /* On BSD-alike OSes elf.h defines these according to host's word size */
  16. #undef ELF_ST_BIND
  17. #undef ELF_ST_TYPE
  18. #undef ELF_R_SYM
  19. #undef ELF_R_TYPE
  20. #if KERNEL_ELFCLASS == ELFCLASS32
  21. #define Elf_Ehdr Elf32_Ehdr
  22. #define Elf_Shdr Elf32_Shdr
  23. #define Elf_Sym Elf32_Sym
  24. #define Elf_Addr Elf32_Addr
  25. #define Elf_Section Elf32_Half
  26. #define ELF_ST_BIND ELF32_ST_BIND
  27. #define ELF_ST_TYPE ELF32_ST_TYPE
  28. #define Elf_Rel Elf32_Rel
  29. #define Elf_Rela Elf32_Rela
  30. #define ELF_R_SYM ELF32_R_SYM
  31. #define ELF_R_TYPE ELF32_R_TYPE
  32. #else
  33. #define Elf_Ehdr Elf64_Ehdr
  34. #define Elf_Shdr Elf64_Shdr
  35. #define Elf_Sym Elf64_Sym
  36. #define Elf_Addr Elf64_Addr
  37. #define Elf_Section Elf64_Half
  38. #define ELF_ST_BIND ELF64_ST_BIND
  39. #define ELF_ST_TYPE ELF64_ST_TYPE
  40. #define Elf_Rel Elf64_Rel
  41. #define Elf_Rela Elf64_Rela
  42. #define ELF_R_SYM ELF64_R_SYM
  43. #define ELF_R_TYPE ELF64_R_TYPE
  44. #endif
  45. /* The 64-bit MIPS ELF ABI uses an unusual reloc format. */
  46. typedef struct
  47. {
  48. Elf32_Word r_sym; /* Symbol index */
  49. unsigned char r_ssym; /* Special symbol for 2nd relocation */
  50. unsigned char r_type3; /* 3rd relocation type */
  51. unsigned char r_type2; /* 2nd relocation type */
  52. unsigned char r_type1; /* 1st relocation type */
  53. } _Elf64_Mips_R_Info;
  54. typedef union
  55. {
  56. Elf64_Xword r_info_number;
  57. _Elf64_Mips_R_Info r_info_fields;
  58. } _Elf64_Mips_R_Info_union;
  59. #define ELF64_MIPS_R_SYM(i) \
  60. ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
  61. #define ELF64_MIPS_R_TYPE(i) \
  62. ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1)
  63. #if KERNEL_ELFDATA != HOST_ELFDATA
  64. static inline void __endian(const void *src, void *dest, unsigned int size)
  65. {
  66. unsigned int i;
  67. for (i = 0; i < size; i++)
  68. ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1];
  69. }
  70. #define TO_NATIVE(x) \
  71. ({ \
  72. typeof(x) __x; \
  73. __endian(&(x), &(__x), sizeof(__x)); \
  74. __x; \
  75. })
  76. #else /* endianness matches */
  77. #define TO_NATIVE(x) (x)
  78. #endif
  79. #define NOFAIL(ptr) do_nofail((ptr), #ptr)
  80. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  81. void *do_nofail(void *ptr, const char *expr);
  82. struct buffer {
  83. char *p;
  84. int pos;
  85. int size;
  86. };
  87. void __attribute__((format(printf, 2, 3)))
  88. buf_printf(struct buffer *buf, const char *fmt, ...);
  89. void
  90. buf_write(struct buffer *buf, const char *s, int len);
  91. struct module {
  92. struct list_head list;
  93. struct list_head exported_symbols;
  94. struct list_head unresolved_symbols;
  95. bool is_gpl_compatible;
  96. bool from_dump; /* true if module was loaded from *.symvers */
  97. bool is_vmlinux;
  98. bool seen;
  99. bool has_init;
  100. bool has_cleanup;
  101. struct buffer dev_table_buf;
  102. char srcversion[25];
  103. // Missing namespace dependencies
  104. struct list_head missing_namespaces;
  105. // Actual imported namespaces
  106. struct list_head imported_namespaces;
  107. char name[];
  108. };
  109. struct elf_info {
  110. size_t size;
  111. Elf_Ehdr *hdr;
  112. Elf_Shdr *sechdrs;
  113. Elf_Sym *symtab_start;
  114. Elf_Sym *symtab_stop;
  115. char *strtab;
  116. char *modinfo;
  117. unsigned int modinfo_len;
  118. /* support for 32bit section numbers */
  119. unsigned int num_sections; /* max_secindex + 1 */
  120. unsigned int secindex_strings;
  121. /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
  122. * take shndx from symtab_shndx_start[N] instead */
  123. Elf32_Word *symtab_shndx_start;
  124. Elf32_Word *symtab_shndx_stop;
  125. };
  126. static inline int is_shndx_special(unsigned int i)
  127. {
  128. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  129. }
  130. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  131. static inline unsigned int get_secindex(const struct elf_info *info,
  132. const Elf_Sym *sym)
  133. {
  134. unsigned int index = sym->st_shndx;
  135. /*
  136. * Elf{32,64}_Sym::st_shndx is 2 byte. Big section numbers are available
  137. * in the .symtab_shndx section.
  138. */
  139. if (index == SHN_XINDEX)
  140. return info->symtab_shndx_start[sym - info->symtab_start];
  141. /*
  142. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  143. * the way to UINT_MAX-255..UINT_MAX, to avoid conflicting with real
  144. * section indices.
  145. */
  146. if (index >= SHN_LORESERVE && index <= SHN_HIRESERVE)
  147. return index - SHN_HIRESERVE - 1;
  148. return index;
  149. }
  150. /* file2alias.c */
  151. void handle_moddevtable(struct module *mod, struct elf_info *info,
  152. Elf_Sym *sym, const char *symname);
  153. void add_moddevtable(struct buffer *buf, struct module *mod);
  154. /* sumversion.c */
  155. void get_src_version(const char *modname, char sum[], unsigned sumlen);
  156. /* from modpost.c */
  157. char *read_text_file(const char *filename);
  158. char *get_line(char **stringp);
  159. void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym);
  160. enum loglevel {
  161. LOG_WARN,
  162. LOG_ERROR,
  163. LOG_FATAL
  164. };
  165. void modpost_log(enum loglevel loglevel, const char *fmt, ...);
  166. /*
  167. * warn - show the given message, then let modpost continue running, still
  168. * allowing modpost to exit successfully. This should be used when
  169. * we still allow to generate vmlinux and modules.
  170. *
  171. * error - show the given message, then let modpost continue running, but fail
  172. * in the end. This should be used when we should stop building vmlinux
  173. * or modules, but we can continue running modpost to catch as many
  174. * issues as possible.
  175. *
  176. * fatal - show the given message, and bail out immediately. This should be
  177. * used when there is no point to continue running modpost.
  178. */
  179. #define warn(fmt, args...) modpost_log(LOG_WARN, fmt, ##args)
  180. #define error(fmt, args...) modpost_log(LOG_ERROR, fmt, ##args)
  181. #define fatal(fmt, args...) modpost_log(LOG_FATAL, fmt, ##args)