modpost.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/mman.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <elf.h>
  11. #include "elfconfig.h"
  12. #if KERNEL_ELFCLASS == ELFCLASS32
  13. #define Elf_Ehdr Elf32_Ehdr
  14. #define Elf_Shdr Elf32_Shdr
  15. #define Elf_Sym Elf32_Sym
  16. #define Elf_Addr Elf32_Addr
  17. #define Elf_Sword Elf64_Sword
  18. #define Elf_Section Elf32_Half
  19. #define ELF_ST_BIND ELF32_ST_BIND
  20. #define ELF_ST_TYPE ELF32_ST_TYPE
  21. #define Elf_Rel Elf32_Rel
  22. #define Elf_Rela Elf32_Rela
  23. #define ELF_R_SYM ELF32_R_SYM
  24. #define ELF_R_TYPE ELF32_R_TYPE
  25. #else
  26. #define Elf_Ehdr Elf64_Ehdr
  27. #define Elf_Shdr Elf64_Shdr
  28. #define Elf_Sym Elf64_Sym
  29. #define Elf_Addr Elf64_Addr
  30. #define Elf_Sword Elf64_Sxword
  31. #define Elf_Section Elf64_Half
  32. #define ELF_ST_BIND ELF64_ST_BIND
  33. #define ELF_ST_TYPE ELF64_ST_TYPE
  34. #define Elf_Rel Elf64_Rel
  35. #define Elf_Rela Elf64_Rela
  36. #define ELF_R_SYM ELF64_R_SYM
  37. #define ELF_R_TYPE ELF64_R_TYPE
  38. #endif
  39. /* The 64-bit MIPS ELF ABI uses an unusual reloc format. */
  40. typedef struct
  41. {
  42. Elf32_Word r_sym; /* Symbol index */
  43. unsigned char r_ssym; /* Special symbol for 2nd relocation */
  44. unsigned char r_type3; /* 3rd relocation type */
  45. unsigned char r_type2; /* 2nd relocation type */
  46. unsigned char r_type1; /* 1st relocation type */
  47. } _Elf64_Mips_R_Info;
  48. typedef union
  49. {
  50. Elf64_Xword r_info_number;
  51. _Elf64_Mips_R_Info r_info_fields;
  52. } _Elf64_Mips_R_Info_union;
  53. #define ELF64_MIPS_R_SYM(i) \
  54. ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
  55. #define ELF64_MIPS_R_TYPE(i) \
  56. ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1)
  57. #if KERNEL_ELFDATA != HOST_ELFDATA
  58. static inline void __endian(const void *src, void *dest, unsigned int size)
  59. {
  60. unsigned int i;
  61. for (i = 0; i < size; i++)
  62. ((unsigned char*)dest)[i] = ((unsigned char*)src)[size - i-1];
  63. }
  64. #define TO_NATIVE(x) \
  65. ({ \
  66. typeof(x) __x; \
  67. __endian(&(x), &(__x), sizeof(__x)); \
  68. __x; \
  69. })
  70. #else /* endianness matches */
  71. #define TO_NATIVE(x) (x)
  72. #endif
  73. #define NOFAIL(ptr) do_nofail((ptr), #ptr)
  74. void *do_nofail(void *ptr, const char *expr);
  75. struct buffer {
  76. char *p;
  77. int pos;
  78. int size;
  79. };
  80. void __attribute__((format(printf, 2, 3)))
  81. buf_printf(struct buffer *buf, const char *fmt, ...);
  82. void
  83. buf_write(struct buffer *buf, const char *s, int len);
  84. struct module {
  85. struct module *next;
  86. const char *name;
  87. int gpl_compatible;
  88. struct symbol *unres;
  89. int seen;
  90. int skip;
  91. int has_init;
  92. int has_cleanup;
  93. struct buffer dev_table_buf;
  94. char srcversion[25];
  95. };
  96. struct elf_info {
  97. unsigned long size;
  98. Elf_Ehdr *hdr;
  99. Elf_Shdr *sechdrs;
  100. Elf_Sym *symtab_start;
  101. Elf_Sym *symtab_stop;
  102. Elf_Section export_sec;
  103. Elf_Section export_unused_sec;
  104. Elf_Section export_gpl_sec;
  105. Elf_Section export_unused_gpl_sec;
  106. Elf_Section export_gpl_future_sec;
  107. const char *strtab;
  108. char *modinfo;
  109. unsigned int modinfo_len;
  110. /* support for 32bit section numbers */
  111. unsigned int num_sections; /* max_secindex + 1 */
  112. unsigned int secindex_strings;
  113. /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
  114. * take shndx from symtab_shndx_start[N] instead */
  115. Elf32_Word *symtab_shndx_start;
  116. Elf32_Word *symtab_shndx_stop;
  117. };
  118. static inline int is_shndx_special(unsigned int i)
  119. {
  120. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  121. }
  122. /* shndx is in [0..SHN_LORESERVE) U (SHN_HIRESERVE, 0xfffffff], thus:
  123. * shndx == 0 <=> sechdrs[0]
  124. * ......
  125. * shndx == SHN_LORESERVE-1 <=> sechdrs[SHN_LORESERVE-1]
  126. * shndx == SHN_HIRESERVE+1 <=> sechdrs[SHN_LORESERVE]
  127. * shndx == SHN_HIRESERVE+2 <=> sechdrs[SHN_LORESERVE+1]
  128. * ......
  129. * fyi: sym->st_shndx is uint16, SHN_LORESERVE = ff00, SHN_HIRESERVE = ffff,
  130. * so basically we map 0000..feff -> 0000..feff
  131. * ff00..ffff -> (you are a bad boy, dont do it)
  132. * 10000..xxxx -> ff00..(xxxx-0x100)
  133. */
  134. static inline unsigned int shndx2secindex(unsigned int i)
  135. {
  136. if (i <= SHN_HIRESERVE)
  137. return i;
  138. return i - (SHN_HIRESERVE + 1 - SHN_LORESERVE);
  139. }
  140. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  141. static inline unsigned int get_secindex(const struct elf_info *info,
  142. const Elf_Sym *sym)
  143. {
  144. if (sym->st_shndx != SHN_XINDEX)
  145. return sym->st_shndx;
  146. return shndx2secindex(info->symtab_shndx_start[sym -
  147. info->symtab_start]);
  148. }
  149. /* file2alias.c */
  150. extern unsigned int cross_build;
  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 maybe_frob_rcs_version(const char *modfilename,
  156. char *version,
  157. void *modinfo,
  158. unsigned long modinfo_offset);
  159. void get_src_version(const char *modname, char sum[], unsigned sumlen);
  160. /* from modpost.c */
  161. void *grab_file(const char *filename, unsigned long *size);
  162. char* get_next_line(unsigned long *pos, void *file, unsigned long size);
  163. void release_file(void *file, unsigned long size);
  164. void fatal(const char *fmt, ...);
  165. void warn(const char *fmt, ...);
  166. void merror(const char *fmt, ...);