modpost.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/mman.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <elf.h>
  12. #include "elfconfig.h"
  13. /* On BSD-alike OSes elf.h defines these according to host's word size */
  14. #undef ELF_ST_BIND
  15. #undef ELF_ST_TYPE
  16. #undef ELF_R_SYM
  17. #undef ELF_R_TYPE
  18. #if KERNEL_ELFCLASS == ELFCLASS32
  19. #define Elf_Ehdr Elf32_Ehdr
  20. #define Elf_Shdr Elf32_Shdr
  21. #define Elf_Sym Elf32_Sym
  22. #define Elf_Addr Elf32_Addr
  23. #define Elf_Sword Elf64_Sword
  24. #define Elf_Section Elf32_Half
  25. #define ELF_ST_BIND ELF32_ST_BIND
  26. #define ELF_ST_TYPE ELF32_ST_TYPE
  27. #define Elf_Rel Elf32_Rel
  28. #define Elf_Rela Elf32_Rela
  29. #define ELF_R_SYM ELF32_R_SYM
  30. #define ELF_R_TYPE ELF32_R_TYPE
  31. #else
  32. #define Elf_Ehdr Elf64_Ehdr
  33. #define Elf_Shdr Elf64_Shdr
  34. #define Elf_Sym Elf64_Sym
  35. #define Elf_Addr Elf64_Addr
  36. #define Elf_Sword Elf64_Sxword
  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. void *do_nofail(void *ptr, const char *expr);
  81. struct buffer {
  82. char *p;
  83. int pos;
  84. int size;
  85. };
  86. void __attribute__((format(printf, 2, 3)))
  87. buf_printf(struct buffer *buf, const char *fmt, ...);
  88. void
  89. buf_write(struct buffer *buf, const char *s, int len);
  90. struct module {
  91. struct module *next;
  92. const char *name;
  93. int gpl_compatible;
  94. struct symbol *unres;
  95. int seen;
  96. int skip;
  97. int has_init;
  98. int has_cleanup;
  99. struct buffer dev_table_buf;
  100. char srcversion[25];
  101. int is_dot_o;
  102. };
  103. struct elf_info {
  104. unsigned long size;
  105. Elf_Ehdr *hdr;
  106. Elf_Shdr *sechdrs;
  107. Elf_Sym *symtab_start;
  108. Elf_Sym *symtab_stop;
  109. Elf_Section export_sec;
  110. Elf_Section export_unused_sec;
  111. Elf_Section export_gpl_sec;
  112. Elf_Section export_unused_gpl_sec;
  113. Elf_Section export_gpl_future_sec;
  114. char *strtab;
  115. char *modinfo;
  116. unsigned int modinfo_len;
  117. /* support for 32bit section numbers */
  118. unsigned int num_sections; /* max_secindex + 1 */
  119. unsigned int secindex_strings;
  120. /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
  121. * take shndx from symtab_shndx_start[N] instead */
  122. Elf32_Word *symtab_shndx_start;
  123. Elf32_Word *symtab_shndx_stop;
  124. };
  125. static inline int is_shndx_special(unsigned int i)
  126. {
  127. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  128. }
  129. /*
  130. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  131. * the way to -256..-1, to avoid conflicting with real section
  132. * indices.
  133. */
  134. #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
  135. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  136. static inline unsigned int get_secindex(const struct elf_info *info,
  137. const Elf_Sym *sym)
  138. {
  139. if (is_shndx_special(sym->st_shndx))
  140. return SPECIAL(sym->st_shndx);
  141. if (sym->st_shndx != SHN_XINDEX)
  142. return sym->st_shndx;
  143. return info->symtab_shndx_start[sym - info->symtab_start];
  144. }
  145. /* file2alias.c */
  146. extern unsigned int cross_build;
  147. void handle_moddevtable(struct module *mod, struct elf_info *info,
  148. Elf_Sym *sym, const char *symname);
  149. void add_moddevtable(struct buffer *buf, struct module *mod);
  150. /* sumversion.c */
  151. void maybe_frob_rcs_version(const char *modfilename,
  152. char *version,
  153. void *modinfo,
  154. unsigned long modinfo_offset);
  155. void get_src_version(const char *modname, char sum[], unsigned sumlen);
  156. /* from modpost.c */
  157. void *grab_file(const char *filename, unsigned long *size);
  158. char* get_next_line(unsigned long *pos, void *file, unsigned long size);
  159. void release_file(void *file, unsigned long size);
  160. void fatal(const char *fmt, ...);
  161. void warn(const char *fmt, ...);
  162. void merror(const char *fmt, ...);