modpost.h 4.8 KB

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