modpost.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. int is_dot_o;
  96. };
  97. struct elf_info {
  98. unsigned long size;
  99. Elf_Ehdr *hdr;
  100. Elf_Shdr *sechdrs;
  101. Elf_Sym *symtab_start;
  102. Elf_Sym *symtab_stop;
  103. Elf_Section export_sec;
  104. Elf_Section export_unused_sec;
  105. Elf_Section export_gpl_sec;
  106. Elf_Section export_unused_gpl_sec;
  107. Elf_Section export_gpl_future_sec;
  108. char *strtab;
  109. char *modinfo;
  110. unsigned int modinfo_len;
  111. /* support for 32bit section numbers */
  112. unsigned int num_sections; /* max_secindex + 1 */
  113. unsigned int secindex_strings;
  114. /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
  115. * take shndx from symtab_shndx_start[N] instead */
  116. Elf32_Word *symtab_shndx_start;
  117. Elf32_Word *symtab_shndx_stop;
  118. };
  119. static inline int is_shndx_special(unsigned int i)
  120. {
  121. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  122. }
  123. /*
  124. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  125. * the way to -256..-1, to avoid conflicting with real section
  126. * indices.
  127. */
  128. #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
  129. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  130. static inline unsigned int get_secindex(const struct elf_info *info,
  131. const Elf_Sym *sym)
  132. {
  133. if (is_shndx_special(sym->st_shndx))
  134. return SPECIAL(sym->st_shndx);
  135. if (sym->st_shndx != SHN_XINDEX)
  136. return sym->st_shndx;
  137. return info->symtab_shndx_start[sym - info->symtab_start];
  138. }
  139. /* file2alias.c */
  140. extern unsigned int cross_build;
  141. void handle_moddevtable(struct module *mod, struct elf_info *info,
  142. Elf_Sym *sym, const char *symname);
  143. void add_moddevtable(struct buffer *buf, struct module *mod);
  144. /* sumversion.c */
  145. void maybe_frob_rcs_version(const char *modfilename,
  146. char *version,
  147. void *modinfo,
  148. unsigned long modinfo_offset);
  149. void get_src_version(const char *modname, char sum[], unsigned sumlen);
  150. /* from modpost.c */
  151. void *grab_file(const char *filename, unsigned long *size);
  152. char* get_next_line(unsigned long *pos, void *file, unsigned long size);
  153. void release_file(void *file, unsigned long size);
  154. void fatal(const char *fmt, ...);
  155. void warn(const char *fmt, ...);
  156. void merror(const char *fmt, ...);