modpost.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /* Postprocess module symbol versions
  2. *
  3. * Copyright 2003 Kai Germaschewski
  4. * Copyright 2002-2004 Rusty Russell, IBM Corporation
  5. * Copyright 2006 Sam Ravnborg
  6. * Based in part on module-init-tools/depmod.c,file2alias
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. * Usage: modpost vmlinux module1.o module2.o ...
  12. */
  13. #include <ctype.h>
  14. #include "modpost.h"
  15. #include "../../include/linux/license.h"
  16. /* Are we using CONFIG_MODVERSIONS? */
  17. int modversions = 0;
  18. /* Warn about undefined symbols? (do so if we have vmlinux) */
  19. int have_vmlinux = 0;
  20. /* Is CONFIG_MODULE_SRCVERSION_ALL set? */
  21. static int all_versions = 0;
  22. /* If we are modposting external module set to 1 */
  23. static int external_module = 0;
  24. /* Warn about section mismatch in vmlinux if set to 1 */
  25. static int vmlinux_section_warnings = 1;
  26. /* Only warn about unresolved symbols */
  27. static int warn_unresolved = 0;
  28. /* How a symbol is exported */
  29. enum export {
  30. export_plain, export_unused, export_gpl,
  31. export_unused_gpl, export_gpl_future, export_unknown
  32. };
  33. void fatal(const char *fmt, ...)
  34. {
  35. va_list arglist;
  36. fprintf(stderr, "FATAL: ");
  37. va_start(arglist, fmt);
  38. vfprintf(stderr, fmt, arglist);
  39. va_end(arglist);
  40. exit(1);
  41. }
  42. void warn(const char *fmt, ...)
  43. {
  44. va_list arglist;
  45. fprintf(stderr, "WARNING: ");
  46. va_start(arglist, fmt);
  47. vfprintf(stderr, fmt, arglist);
  48. va_end(arglist);
  49. }
  50. void merror(const char *fmt, ...)
  51. {
  52. va_list arglist;
  53. fprintf(stderr, "ERROR: ");
  54. va_start(arglist, fmt);
  55. vfprintf(stderr, fmt, arglist);
  56. va_end(arglist);
  57. }
  58. static int is_vmlinux(const char *modname)
  59. {
  60. const char *myname;
  61. if ((myname = strrchr(modname, '/')))
  62. myname++;
  63. else
  64. myname = modname;
  65. return (strcmp(myname, "vmlinux") == 0) ||
  66. (strcmp(myname, "vmlinux.o") == 0);
  67. }
  68. void *do_nofail(void *ptr, const char *expr)
  69. {
  70. if (!ptr) {
  71. fatal("modpost: Memory allocation failure: %s.\n", expr);
  72. }
  73. return ptr;
  74. }
  75. /* A list of all modules we processed */
  76. static struct module *modules;
  77. static struct module *find_module(char *modname)
  78. {
  79. struct module *mod;
  80. for (mod = modules; mod; mod = mod->next)
  81. if (strcmp(mod->name, modname) == 0)
  82. break;
  83. return mod;
  84. }
  85. static struct module *new_module(char *modname)
  86. {
  87. struct module *mod;
  88. char *p, *s;
  89. mod = NOFAIL(malloc(sizeof(*mod)));
  90. memset(mod, 0, sizeof(*mod));
  91. p = NOFAIL(strdup(modname));
  92. /* strip trailing .o */
  93. if ((s = strrchr(p, '.')) != NULL)
  94. if (strcmp(s, ".o") == 0)
  95. *s = '\0';
  96. /* add to list */
  97. mod->name = p;
  98. mod->gpl_compatible = -1;
  99. mod->next = modules;
  100. modules = mod;
  101. return mod;
  102. }
  103. /* A hash of all exported symbols,
  104. * struct symbol is also used for lists of unresolved symbols */
  105. #define SYMBOL_HASH_SIZE 1024
  106. struct symbol {
  107. struct symbol *next;
  108. struct module *module;
  109. unsigned int crc;
  110. int crc_valid;
  111. unsigned int weak:1;
  112. unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
  113. unsigned int kernel:1; /* 1 if symbol is from kernel
  114. * (only for external modules) **/
  115. unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
  116. enum export export; /* Type of export */
  117. char name[0];
  118. };
  119. static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
  120. /* This is based on the hash agorithm from gdbm, via tdb */
  121. static inline unsigned int tdb_hash(const char *name)
  122. {
  123. unsigned value; /* Used to compute the hash value. */
  124. unsigned i; /* Used to cycle through random values. */
  125. /* Set the initial value from the key size. */
  126. for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
  127. value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
  128. return (1103515243 * value + 12345);
  129. }
  130. /**
  131. * Allocate a new symbols for use in the hash of exported symbols or
  132. * the list of unresolved symbols per module
  133. **/
  134. static struct symbol *alloc_symbol(const char *name, unsigned int weak,
  135. struct symbol *next)
  136. {
  137. struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
  138. memset(s, 0, sizeof(*s));
  139. strcpy(s->name, name);
  140. s->weak = weak;
  141. s->next = next;
  142. return s;
  143. }
  144. /* For the hash of exported symbols */
  145. static struct symbol *new_symbol(const char *name, struct module *module,
  146. enum export export)
  147. {
  148. unsigned int hash;
  149. struct symbol *new;
  150. hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
  151. new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
  152. new->module = module;
  153. new->export = export;
  154. return new;
  155. }
  156. static struct symbol *find_symbol(const char *name)
  157. {
  158. struct symbol *s;
  159. /* For our purposes, .foo matches foo. PPC64 needs this. */
  160. if (name[0] == '.')
  161. name++;
  162. for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
  163. if (strcmp(s->name, name) == 0)
  164. return s;
  165. }
  166. return NULL;
  167. }
  168. static struct {
  169. const char *str;
  170. enum export export;
  171. } export_list[] = {
  172. { .str = "EXPORT_SYMBOL", .export = export_plain },
  173. { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
  174. { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
  175. { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
  176. { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
  177. { .str = "(unknown)", .export = export_unknown },
  178. };
  179. static const char *export_str(enum export ex)
  180. {
  181. return export_list[ex].str;
  182. }
  183. static enum export export_no(const char * s)
  184. {
  185. int i;
  186. if (!s)
  187. return export_unknown;
  188. for (i = 0; export_list[i].export != export_unknown; i++) {
  189. if (strcmp(export_list[i].str, s) == 0)
  190. return export_list[i].export;
  191. }
  192. return export_unknown;
  193. }
  194. static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
  195. {
  196. if (sec == elf->export_sec)
  197. return export_plain;
  198. else if (sec == elf->export_unused_sec)
  199. return export_unused;
  200. else if (sec == elf->export_gpl_sec)
  201. return export_gpl;
  202. else if (sec == elf->export_unused_gpl_sec)
  203. return export_unused_gpl;
  204. else if (sec == elf->export_gpl_future_sec)
  205. return export_gpl_future;
  206. else
  207. return export_unknown;
  208. }
  209. /**
  210. * Add an exported symbol - it may have already been added without a
  211. * CRC, in this case just update the CRC
  212. **/
  213. static struct symbol *sym_add_exported(const char *name, struct module *mod,
  214. enum export export)
  215. {
  216. struct symbol *s = find_symbol(name);
  217. if (!s) {
  218. s = new_symbol(name, mod, export);
  219. } else {
  220. if (!s->preloaded) {
  221. warn("%s: '%s' exported twice. Previous export "
  222. "was in %s%s\n", mod->name, name,
  223. s->module->name,
  224. is_vmlinux(s->module->name) ?"":".ko");
  225. }
  226. }
  227. s->preloaded = 0;
  228. s->vmlinux = is_vmlinux(mod->name);
  229. s->kernel = 0;
  230. s->export = export;
  231. return s;
  232. }
  233. static void sym_update_crc(const char *name, struct module *mod,
  234. unsigned int crc, enum export export)
  235. {
  236. struct symbol *s = find_symbol(name);
  237. if (!s)
  238. s = new_symbol(name, mod, export);
  239. s->crc = crc;
  240. s->crc_valid = 1;
  241. }
  242. void *grab_file(const char *filename, unsigned long *size)
  243. {
  244. struct stat st;
  245. void *map;
  246. int fd;
  247. fd = open(filename, O_RDONLY);
  248. if (fd < 0 || fstat(fd, &st) != 0)
  249. return NULL;
  250. *size = st.st_size;
  251. map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  252. close(fd);
  253. if (map == MAP_FAILED)
  254. return NULL;
  255. return map;
  256. }
  257. /**
  258. * Return a copy of the next line in a mmap'ed file.
  259. * spaces in the beginning of the line is trimmed away.
  260. * Return a pointer to a static buffer.
  261. **/
  262. char* get_next_line(unsigned long *pos, void *file, unsigned long size)
  263. {
  264. static char line[4096];
  265. int skip = 1;
  266. size_t len = 0;
  267. signed char *p = (signed char *)file + *pos;
  268. char *s = line;
  269. for (; *pos < size ; (*pos)++)
  270. {
  271. if (skip && isspace(*p)) {
  272. p++;
  273. continue;
  274. }
  275. skip = 0;
  276. if (*p != '\n' && (*pos < size)) {
  277. len++;
  278. *s++ = *p++;
  279. if (len > 4095)
  280. break; /* Too long, stop */
  281. } else {
  282. /* End of string */
  283. *s = '\0';
  284. return line;
  285. }
  286. }
  287. /* End of buffer */
  288. return NULL;
  289. }
  290. void release_file(void *file, unsigned long size)
  291. {
  292. munmap(file, size);
  293. }
  294. static int parse_elf(struct elf_info *info, const char *filename)
  295. {
  296. unsigned int i;
  297. Elf_Ehdr *hdr;
  298. Elf_Shdr *sechdrs;
  299. Elf_Sym *sym;
  300. hdr = grab_file(filename, &info->size);
  301. if (!hdr) {
  302. perror(filename);
  303. exit(1);
  304. }
  305. info->hdr = hdr;
  306. if (info->size < sizeof(*hdr)) {
  307. /* file too small, assume this is an empty .o file */
  308. return 0;
  309. }
  310. /* Is this a valid ELF file? */
  311. if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
  312. (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
  313. (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
  314. (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
  315. /* Not an ELF file - silently ignore it */
  316. return 0;
  317. }
  318. /* Fix endianness in ELF header */
  319. hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
  320. hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
  321. hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
  322. hdr->e_machine = TO_NATIVE(hdr->e_machine);
  323. hdr->e_type = TO_NATIVE(hdr->e_type);
  324. sechdrs = (void *)hdr + hdr->e_shoff;
  325. info->sechdrs = sechdrs;
  326. /* Fix endianness in section headers */
  327. for (i = 0; i < hdr->e_shnum; i++) {
  328. sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
  329. sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
  330. sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
  331. sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
  332. sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
  333. sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
  334. sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
  335. }
  336. /* Find symbol table. */
  337. for (i = 1; i < hdr->e_shnum; i++) {
  338. const char *secstrings
  339. = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  340. const char *secname;
  341. if (sechdrs[i].sh_offset > info->size) {
  342. fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
  343. return 0;
  344. }
  345. secname = secstrings + sechdrs[i].sh_name;
  346. if (strcmp(secname, ".modinfo") == 0) {
  347. info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
  348. info->modinfo_len = sechdrs[i].sh_size;
  349. } else if (strcmp(secname, "__ksymtab") == 0)
  350. info->export_sec = i;
  351. else if (strcmp(secname, "__ksymtab_unused") == 0)
  352. info->export_unused_sec = i;
  353. else if (strcmp(secname, "__ksymtab_gpl") == 0)
  354. info->export_gpl_sec = i;
  355. else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
  356. info->export_unused_gpl_sec = i;
  357. else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
  358. info->export_gpl_future_sec = i;
  359. if (sechdrs[i].sh_type != SHT_SYMTAB)
  360. continue;
  361. info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
  362. info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
  363. + sechdrs[i].sh_size;
  364. info->strtab = (void *)hdr +
  365. sechdrs[sechdrs[i].sh_link].sh_offset;
  366. }
  367. if (!info->symtab_start) {
  368. fatal("%s has no symtab?\n", filename);
  369. }
  370. /* Fix endianness in symbols */
  371. for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
  372. sym->st_shndx = TO_NATIVE(sym->st_shndx);
  373. sym->st_name = TO_NATIVE(sym->st_name);
  374. sym->st_value = TO_NATIVE(sym->st_value);
  375. sym->st_size = TO_NATIVE(sym->st_size);
  376. }
  377. return 1;
  378. }
  379. static void parse_elf_finish(struct elf_info *info)
  380. {
  381. release_file(info->hdr, info->size);
  382. }
  383. #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
  384. #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
  385. static void handle_modversions(struct module *mod, struct elf_info *info,
  386. Elf_Sym *sym, const char *symname)
  387. {
  388. unsigned int crc;
  389. enum export export = export_from_sec(info, sym->st_shndx);
  390. switch (sym->st_shndx) {
  391. case SHN_COMMON:
  392. warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
  393. break;
  394. case SHN_ABS:
  395. /* CRC'd symbol */
  396. if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
  397. crc = (unsigned int) sym->st_value;
  398. sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
  399. export);
  400. }
  401. break;
  402. case SHN_UNDEF:
  403. /* undefined symbol */
  404. if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
  405. ELF_ST_BIND(sym->st_info) != STB_WEAK)
  406. break;
  407. /* ignore global offset table */
  408. if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
  409. break;
  410. /* ignore __this_module, it will be resolved shortly */
  411. if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
  412. break;
  413. /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
  414. #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
  415. /* add compatibility with older glibc */
  416. #ifndef STT_SPARC_REGISTER
  417. #define STT_SPARC_REGISTER STT_REGISTER
  418. #endif
  419. if (info->hdr->e_machine == EM_SPARC ||
  420. info->hdr->e_machine == EM_SPARCV9) {
  421. /* Ignore register directives. */
  422. if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
  423. break;
  424. if (symname[0] == '.') {
  425. char *munged = strdup(symname);
  426. munged[0] = '_';
  427. munged[1] = toupper(munged[1]);
  428. symname = munged;
  429. }
  430. }
  431. #endif
  432. if (memcmp(symname, MODULE_SYMBOL_PREFIX,
  433. strlen(MODULE_SYMBOL_PREFIX)) == 0)
  434. mod->unres = alloc_symbol(symname +
  435. strlen(MODULE_SYMBOL_PREFIX),
  436. ELF_ST_BIND(sym->st_info) == STB_WEAK,
  437. mod->unres);
  438. break;
  439. default:
  440. /* All exported symbols */
  441. if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
  442. sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
  443. export);
  444. }
  445. if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
  446. mod->has_init = 1;
  447. if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
  448. mod->has_cleanup = 1;
  449. break;
  450. }
  451. }
  452. /**
  453. * Parse tag=value strings from .modinfo section
  454. **/
  455. static char *next_string(char *string, unsigned long *secsize)
  456. {
  457. /* Skip non-zero chars */
  458. while (string[0]) {
  459. string++;
  460. if ((*secsize)-- <= 1)
  461. return NULL;
  462. }
  463. /* Skip any zero padding. */
  464. while (!string[0]) {
  465. string++;
  466. if ((*secsize)-- <= 1)
  467. return NULL;
  468. }
  469. return string;
  470. }
  471. static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
  472. const char *tag, char *info)
  473. {
  474. char *p;
  475. unsigned int taglen = strlen(tag);
  476. unsigned long size = modinfo_len;
  477. if (info) {
  478. size -= info - (char *)modinfo;
  479. modinfo = next_string(info, &size);
  480. }
  481. for (p = modinfo; p; p = next_string(p, &size)) {
  482. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  483. return p + taglen + 1;
  484. }
  485. return NULL;
  486. }
  487. static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
  488. const char *tag)
  489. {
  490. return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
  491. }
  492. /**
  493. * Test if string s ends in string sub
  494. * return 0 if match
  495. **/
  496. static int strrcmp(const char *s, const char *sub)
  497. {
  498. int slen, sublen;
  499. if (!s || !sub)
  500. return 1;
  501. slen = strlen(s);
  502. sublen = strlen(sub);
  503. if ((slen == 0) || (sublen == 0))
  504. return 1;
  505. if (sublen > slen)
  506. return 1;
  507. return memcmp(s + slen - sublen, sub, sublen);
  508. }
  509. /*
  510. * Functions used only during module init is marked __init and is stored in
  511. * a .init.text section. Likewise data is marked __initdata and stored in
  512. * a .init.data section.
  513. * If this section is one of these sections return 1
  514. * See include/linux/init.h for the details
  515. */
  516. static int init_section(const char *name)
  517. {
  518. if (strcmp(name, ".init") == 0)
  519. return 1;
  520. if (strncmp(name, ".init.", strlen(".init.")) == 0)
  521. return 1;
  522. return 0;
  523. }
  524. /*
  525. * Functions used only during module exit is marked __exit and is stored in
  526. * a .exit.text section. Likewise data is marked __exitdata and stored in
  527. * a .exit.data section.
  528. * If this section is one of these sections return 1
  529. * See include/linux/init.h for the details
  530. **/
  531. static int exit_section(const char *name)
  532. {
  533. if (strcmp(name, ".exit.text") == 0)
  534. return 1;
  535. if (strcmp(name, ".exit.data") == 0)
  536. return 1;
  537. return 0;
  538. }
  539. /*
  540. * Data sections are named like this:
  541. * .data | .data.rel | .data.rel.*
  542. * Return 1 if the specified section is a data section
  543. */
  544. static int data_section(const char *name)
  545. {
  546. if ((strcmp(name, ".data") == 0) ||
  547. (strcmp(name, ".data.rel") == 0) ||
  548. (strncmp(name, ".data.rel.", strlen(".data.rel.")) == 0))
  549. return 1;
  550. else
  551. return 0;
  552. }
  553. /**
  554. * Whitelist to allow certain references to pass with no warning.
  555. *
  556. * Pattern 0:
  557. * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
  558. * The pattern is identified by:
  559. * fromsec = .text.init.refok | .data.init.refok
  560. *
  561. * Pattern 1:
  562. * If a module parameter is declared __initdata and permissions=0
  563. * then this is legal despite the warning generated.
  564. * We cannot see value of permissions here, so just ignore
  565. * this pattern.
  566. * The pattern is identified by:
  567. * tosec = .init.data
  568. * fromsec = .data*
  569. * atsym =__param*
  570. *
  571. * Pattern 2:
  572. * Many drivers utilise a *driver container with references to
  573. * add, remove, probe functions etc.
  574. * These functions may often be marked __init and we do not want to
  575. * warn here.
  576. * the pattern is identified by:
  577. * tosec = .init.text | .exit.text | .init.data
  578. * fromsec = .data | .data.rel | .data.rel.*
  579. * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer
  580. *
  581. * Pattern 3:
  582. * Whitelist all refereces from .text.head to .init.data
  583. * Whitelist all refereces from .text.head to .init.text
  584. *
  585. * Pattern 4:
  586. * Some symbols belong to init section but still it is ok to reference
  587. * these from non-init sections as these symbols don't have any memory
  588. * allocated for them and symbol address and value are same. So even
  589. * if init section is freed, its ok to reference those symbols.
  590. * For ex. symbols marking the init section boundaries.
  591. * This pattern is identified by
  592. * refsymname = __init_begin, _sinittext, _einittext
  593. *
  594. **/
  595. static int secref_whitelist(const char *modname, const char *tosec,
  596. const char *fromsec, const char *atsym,
  597. const char *refsymname)
  598. {
  599. int f1 = 1, f2 = 1;
  600. const char **s;
  601. const char *pat2sym[] = {
  602. "driver",
  603. "_template", /* scsi uses *_template a lot */
  604. "_timer", /* arm uses ops structures named _timer a lot */
  605. "_sht", /* scsi also used *_sht to some extent */
  606. "_ops",
  607. "_probe",
  608. "_probe_one",
  609. "_console",
  610. NULL
  611. };
  612. const char *pat3refsym[] = {
  613. "__init_begin",
  614. "_sinittext",
  615. "_einittext",
  616. NULL
  617. };
  618. /* Check for pattern 0 */
  619. if ((strcmp(fromsec, ".text.init.refok") == 0) ||
  620. (strcmp(fromsec, ".data.init.refok") == 0))
  621. return 1;
  622. /* Check for pattern 1 */
  623. if (strcmp(tosec, ".init.data") != 0)
  624. f1 = 0;
  625. if (strncmp(fromsec, ".data", strlen(".data")) != 0)
  626. f1 = 0;
  627. if (strncmp(atsym, "__param", strlen("__param")) != 0)
  628. f1 = 0;
  629. if (f1)
  630. return f1;
  631. /* Check for pattern 2 */
  632. if ((strcmp(tosec, ".init.text") != 0) &&
  633. (strcmp(tosec, ".exit.text") != 0) &&
  634. (strcmp(tosec, ".init.data") != 0))
  635. f2 = 0;
  636. if ((strcmp(fromsec, ".data") != 0) &&
  637. (strcmp(fromsec, ".data.rel") != 0) &&
  638. (strncmp(fromsec, ".data.rel.", strlen(".data.rel.")) != 0))
  639. f2 = 0;
  640. for (s = pat2sym; *s; s++)
  641. if (strrcmp(atsym, *s) == 0)
  642. f1 = 1;
  643. if (f1 && f2)
  644. return 1;
  645. /* Check for pattern 3 */
  646. if ((strcmp(fromsec, ".text.head") == 0) &&
  647. ((strcmp(tosec, ".init.data") == 0) ||
  648. (strcmp(tosec, ".init.text") == 0)))
  649. return 1;
  650. /* Check for pattern 4 */
  651. for (s = pat3refsym; *s; s++)
  652. if (strcmp(refsymname, *s) == 0)
  653. return 1;
  654. return 0;
  655. }
  656. /**
  657. * Find symbol based on relocation record info.
  658. * In some cases the symbol supplied is a valid symbol so
  659. * return refsym. If st_name != 0 we assume this is a valid symbol.
  660. * In other cases the symbol needs to be looked up in the symbol table
  661. * based on section and address.
  662. * **/
  663. static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
  664. Elf_Sym *relsym)
  665. {
  666. Elf_Sym *sym;
  667. if (relsym->st_name != 0)
  668. return relsym;
  669. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  670. if (sym->st_shndx != relsym->st_shndx)
  671. continue;
  672. if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
  673. continue;
  674. if (sym->st_value == addr)
  675. return sym;
  676. }
  677. return NULL;
  678. }
  679. static inline int is_arm_mapping_symbol(const char *str)
  680. {
  681. return str[0] == '$' && strchr("atd", str[1])
  682. && (str[2] == '\0' || str[2] == '.');
  683. }
  684. /*
  685. * If there's no name there, ignore it; likewise, ignore it if it's
  686. * one of the magic symbols emitted used by current ARM tools.
  687. *
  688. * Otherwise if find_symbols_between() returns those symbols, they'll
  689. * fail the whitelist tests and cause lots of false alarms ... fixable
  690. * only by merging __exit and __init sections into __text, bloating
  691. * the kernel (which is especially evil on embedded platforms).
  692. */
  693. static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
  694. {
  695. const char *name = elf->strtab + sym->st_name;
  696. if (!name || !strlen(name))
  697. return 0;
  698. return !is_arm_mapping_symbol(name);
  699. }
  700. /*
  701. * Find symbols before or equal addr and after addr - in the section sec.
  702. * If we find two symbols with equal offset prefer one with a valid name.
  703. * The ELF format may have a better way to detect what type of symbol
  704. * it is, but this works for now.
  705. **/
  706. static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
  707. const char *sec,
  708. Elf_Sym **before, Elf_Sym **after)
  709. {
  710. Elf_Sym *sym;
  711. Elf_Ehdr *hdr = elf->hdr;
  712. Elf_Addr beforediff = ~0;
  713. Elf_Addr afterdiff = ~0;
  714. const char *secstrings = (void *)hdr +
  715. elf->sechdrs[hdr->e_shstrndx].sh_offset;
  716. *before = NULL;
  717. *after = NULL;
  718. for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
  719. const char *symsec;
  720. if (sym->st_shndx >= SHN_LORESERVE)
  721. continue;
  722. symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
  723. if (strcmp(symsec, sec) != 0)
  724. continue;
  725. if (!is_valid_name(elf, sym))
  726. continue;
  727. if (sym->st_value <= addr) {
  728. if ((addr - sym->st_value) < beforediff) {
  729. beforediff = addr - sym->st_value;
  730. *before = sym;
  731. }
  732. else if ((addr - sym->st_value) == beforediff) {
  733. *before = sym;
  734. }
  735. }
  736. else
  737. {
  738. if ((sym->st_value - addr) < afterdiff) {
  739. afterdiff = sym->st_value - addr;
  740. *after = sym;
  741. }
  742. else if ((sym->st_value - addr) == afterdiff) {
  743. *after = sym;
  744. }
  745. }
  746. }
  747. }
  748. /**
  749. * Print a warning about a section mismatch.
  750. * Try to find symbols near it so user can find it.
  751. * Check whitelist before warning - it may be a false positive.
  752. **/
  753. static void warn_sec_mismatch(const char *modname, const char *fromsec,
  754. struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
  755. {
  756. const char *refsymname = "";
  757. Elf_Sym *before, *after;
  758. Elf_Sym *refsym;
  759. Elf_Ehdr *hdr = elf->hdr;
  760. Elf_Shdr *sechdrs = elf->sechdrs;
  761. const char *secstrings = (void *)hdr +
  762. sechdrs[hdr->e_shstrndx].sh_offset;
  763. const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
  764. find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
  765. refsym = find_elf_symbol(elf, r.r_addend, sym);
  766. if (refsym && strlen(elf->strtab + refsym->st_name))
  767. refsymname = elf->strtab + refsym->st_name;
  768. /* check whitelist - we may ignore it */
  769. if (before &&
  770. secref_whitelist(modname, secname, fromsec,
  771. elf->strtab + before->st_name, refsymname))
  772. return;
  773. if (before && after) {
  774. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  775. "(between '%s' and '%s')\n",
  776. modname, fromsec, (unsigned long long)r.r_offset,
  777. secname, refsymname,
  778. elf->strtab + before->st_name,
  779. elf->strtab + after->st_name);
  780. } else if (before) {
  781. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  782. "(after '%s')\n",
  783. modname, fromsec, (unsigned long long)r.r_offset,
  784. secname, refsymname,
  785. elf->strtab + before->st_name);
  786. } else if (after) {
  787. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
  788. "before '%s' (at offset -0x%llx)\n",
  789. modname, fromsec, (unsigned long long)r.r_offset,
  790. secname, refsymname,
  791. elf->strtab + after->st_name);
  792. } else {
  793. warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
  794. modname, fromsec, (unsigned long long)r.r_offset,
  795. secname, refsymname);
  796. }
  797. }
  798. static unsigned int *reloc_location(struct elf_info *elf,
  799. int rsection, Elf_Rela *r)
  800. {
  801. Elf_Shdr *sechdrs = elf->sechdrs;
  802. int section = sechdrs[rsection].sh_info;
  803. return (void *)elf->hdr + sechdrs[section].sh_offset +
  804. (r->r_offset - sechdrs[section].sh_addr);
  805. }
  806. static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  807. {
  808. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  809. unsigned int *location = reloc_location(elf, rsection, r);
  810. switch (r_typ) {
  811. case R_386_32:
  812. r->r_addend = TO_NATIVE(*location);
  813. break;
  814. case R_386_PC32:
  815. r->r_addend = TO_NATIVE(*location) + 4;
  816. /* For CONFIG_RELOCATABLE=y */
  817. if (elf->hdr->e_type == ET_EXEC)
  818. r->r_addend += r->r_offset;
  819. break;
  820. }
  821. return 0;
  822. }
  823. static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  824. {
  825. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  826. switch (r_typ) {
  827. case R_ARM_ABS32:
  828. /* From ARM ABI: (S + A) | T */
  829. r->r_addend = (int)(long)(elf->symtab_start + ELF_R_SYM(r->r_info));
  830. break;
  831. case R_ARM_PC24:
  832. /* From ARM ABI: ((S + A) | T) - P */
  833. r->r_addend = (int)(long)(elf->hdr + elf->sechdrs[rsection].sh_offset +
  834. (r->r_offset - elf->sechdrs[rsection].sh_addr));
  835. break;
  836. default:
  837. return 1;
  838. }
  839. return 0;
  840. }
  841. static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
  842. {
  843. unsigned int r_typ = ELF_R_TYPE(r->r_info);
  844. unsigned int *location = reloc_location(elf, rsection, r);
  845. unsigned int inst;
  846. if (r_typ == R_MIPS_HI16)
  847. return 1; /* skip this */
  848. inst = TO_NATIVE(*location);
  849. switch (r_typ) {
  850. case R_MIPS_LO16:
  851. r->r_addend = inst & 0xffff;
  852. break;
  853. case R_MIPS_26:
  854. r->r_addend = (inst & 0x03ffffff) << 2;
  855. break;
  856. case R_MIPS_32:
  857. r->r_addend = inst;
  858. break;
  859. }
  860. return 0;
  861. }
  862. /**
  863. * A module includes a number of sections that are discarded
  864. * either when loaded or when used as built-in.
  865. * For loaded modules all functions marked __init and all data
  866. * marked __initdata will be discarded when the module has been intialized.
  867. * Likewise for modules used built-in the sections marked __exit
  868. * are discarded because __exit marked function are supposed to be called
  869. * only when a moduel is unloaded which never happes for built-in modules.
  870. * The check_sec_ref() function traverses all relocation records
  871. * to find all references to a section that reference a section that will
  872. * be discarded and warns about it.
  873. **/
  874. static void check_sec_ref(struct module *mod, const char *modname,
  875. struct elf_info *elf,
  876. int section(const char*),
  877. int section_ref_ok(const char *))
  878. {
  879. int i;
  880. Elf_Sym *sym;
  881. Elf_Ehdr *hdr = elf->hdr;
  882. Elf_Shdr *sechdrs = elf->sechdrs;
  883. const char *secstrings = (void *)hdr +
  884. sechdrs[hdr->e_shstrndx].sh_offset;
  885. /* Walk through all sections */
  886. for (i = 0; i < hdr->e_shnum; i++) {
  887. const char *name = secstrings + sechdrs[i].sh_name;
  888. const char *secname;
  889. Elf_Rela r;
  890. unsigned int r_sym;
  891. /* We want to process only relocation sections and not .init */
  892. if (sechdrs[i].sh_type == SHT_RELA) {
  893. Elf_Rela *rela;
  894. Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
  895. Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
  896. name += strlen(".rela");
  897. if (section_ref_ok(name))
  898. continue;
  899. for (rela = start; rela < stop; rela++) {
  900. r.r_offset = TO_NATIVE(rela->r_offset);
  901. #if KERNEL_ELFCLASS == ELFCLASS64
  902. if (hdr->e_machine == EM_MIPS) {
  903. unsigned int r_typ;
  904. r_sym = ELF64_MIPS_R_SYM(rela->r_info);
  905. r_sym = TO_NATIVE(r_sym);
  906. r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
  907. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  908. } else {
  909. r.r_info = TO_NATIVE(rela->r_info);
  910. r_sym = ELF_R_SYM(r.r_info);
  911. }
  912. #else
  913. r.r_info = TO_NATIVE(rela->r_info);
  914. r_sym = ELF_R_SYM(r.r_info);
  915. #endif
  916. r.r_addend = TO_NATIVE(rela->r_addend);
  917. sym = elf->symtab_start + r_sym;
  918. /* Skip special sections */
  919. if (sym->st_shndx >= SHN_LORESERVE)
  920. continue;
  921. secname = secstrings +
  922. sechdrs[sym->st_shndx].sh_name;
  923. if (section(secname))
  924. warn_sec_mismatch(modname, name,
  925. elf, sym, r);
  926. }
  927. } else if (sechdrs[i].sh_type == SHT_REL) {
  928. Elf_Rel *rel;
  929. Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
  930. Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
  931. name += strlen(".rel");
  932. if (section_ref_ok(name))
  933. continue;
  934. for (rel = start; rel < stop; rel++) {
  935. r.r_offset = TO_NATIVE(rel->r_offset);
  936. #if KERNEL_ELFCLASS == ELFCLASS64
  937. if (hdr->e_machine == EM_MIPS) {
  938. unsigned int r_typ;
  939. r_sym = ELF64_MIPS_R_SYM(rel->r_info);
  940. r_sym = TO_NATIVE(r_sym);
  941. r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
  942. r.r_info = ELF64_R_INFO(r_sym, r_typ);
  943. } else {
  944. r.r_info = TO_NATIVE(rel->r_info);
  945. r_sym = ELF_R_SYM(r.r_info);
  946. }
  947. #else
  948. r.r_info = TO_NATIVE(rel->r_info);
  949. r_sym = ELF_R_SYM(r.r_info);
  950. #endif
  951. r.r_addend = 0;
  952. switch (hdr->e_machine) {
  953. case EM_386:
  954. if (addend_386_rel(elf, i, &r))
  955. continue;
  956. break;
  957. case EM_ARM:
  958. if(addend_arm_rel(elf, i, &r))
  959. continue;
  960. break;
  961. case EM_MIPS:
  962. if (addend_mips_rel(elf, i, &r))
  963. continue;
  964. break;
  965. }
  966. sym = elf->symtab_start + r_sym;
  967. /* Skip special sections */
  968. if (sym->st_shndx >= SHN_LORESERVE)
  969. continue;
  970. secname = secstrings +
  971. sechdrs[sym->st_shndx].sh_name;
  972. if (section(secname))
  973. warn_sec_mismatch(modname, name,
  974. elf, sym, r);
  975. }
  976. }
  977. }
  978. }
  979. /*
  980. * Identify sections from which references to either a
  981. * .init or a .exit section is OK.
  982. *
  983. * [OPD] Keith Ownes <kaos@sgi.com> commented:
  984. * For our future {in}sanity, add a comment that this is the ppc .opd
  985. * section, not the ia64 .opd section.
  986. * ia64 .opd should not point to discarded sections.
  987. * [.rodata] like for .init.text we ignore .rodata references -same reason
  988. */
  989. static int initexit_section_ref_ok(const char *name)
  990. {
  991. const char **s;
  992. /* Absolute section names */
  993. const char *namelist1[] = {
  994. "__bug_table", /* used by powerpc for BUG() */
  995. "__ex_table",
  996. ".altinstructions",
  997. ".cranges", /* used by sh64 */
  998. ".fixup",
  999. ".machvec", /* ia64 + powerpc uses these */
  1000. ".machine.desc",
  1001. ".opd", /* See comment [OPD] */
  1002. ".parainstructions",
  1003. ".pdr",
  1004. ".plt", /* seen on ARCH=um build on x86_64. Harmless */
  1005. ".smp_locks",
  1006. ".stab",
  1007. ".m68k_fixup",
  1008. NULL
  1009. };
  1010. /* Start of section names */
  1011. const char *namelist2[] = {
  1012. ".debug",
  1013. ".eh_frame",
  1014. ".note", /* ignore ELF notes - may contain anything */
  1015. ".got", /* powerpc - global offset table */
  1016. ".toc", /* powerpc - table of contents */
  1017. NULL
  1018. };
  1019. /* part of section name */
  1020. const char *namelist3 [] = {
  1021. ".unwind", /* Sample: IA_64.unwind.exit.text */
  1022. NULL
  1023. };
  1024. for (s = namelist1; *s; s++)
  1025. if (strcmp(*s, name) == 0)
  1026. return 1;
  1027. for (s = namelist2; *s; s++)
  1028. if (strncmp(*s, name, strlen(*s)) == 0)
  1029. return 1;
  1030. for (s = namelist3; *s; s++)
  1031. if (strstr(name, *s) != NULL)
  1032. return 1;
  1033. return 0;
  1034. }
  1035. /*
  1036. * Identify sections from which references to a .init section is OK.
  1037. *
  1038. * Unfortunately references to read only data that referenced .init
  1039. * sections had to be excluded. Almost all of these are false
  1040. * positives, they are created by gcc. The downside of excluding rodata
  1041. * is that there really are some user references from rodata to
  1042. * init code, e.g. drivers/video/vgacon.c:
  1043. *
  1044. * const struct consw vga_con = {
  1045. * con_startup: vgacon_startup,
  1046. *
  1047. * where vgacon_startup is __init. If you want to wade through the false
  1048. * positives, take out the check for rodata.
  1049. */
  1050. static int init_section_ref_ok(const char *name)
  1051. {
  1052. const char **s;
  1053. /* Absolute section names */
  1054. const char *namelist1[] = {
  1055. "__dbe_table", /* MIPS generate these */
  1056. "__ftr_fixup", /* powerpc cpu feature fixup */
  1057. "__fw_ftr_fixup", /* powerpc firmware feature fixup */
  1058. "__param",
  1059. ".data.rel.ro", /* used by parisc64 */
  1060. ".init",
  1061. ".text.lock",
  1062. NULL
  1063. };
  1064. /* Start of section names */
  1065. const char *namelist2[] = {
  1066. ".init.",
  1067. ".pci_fixup",
  1068. ".rodata",
  1069. NULL
  1070. };
  1071. if (initexit_section_ref_ok(name))
  1072. return 1;
  1073. for (s = namelist1; *s; s++)
  1074. if (strcmp(*s, name) == 0)
  1075. return 1;
  1076. for (s = namelist2; *s; s++)
  1077. if (strncmp(*s, name, strlen(*s)) == 0)
  1078. return 1;
  1079. /* If section name ends with ".init" we allow references
  1080. * as is the case with .initcallN.init, .early_param.init, .taglist.init etc
  1081. */
  1082. if (strrcmp(name, ".init") == 0)
  1083. return 1;
  1084. return 0;
  1085. }
  1086. /*
  1087. * Identify sections from which references to a .exit section is OK.
  1088. */
  1089. static int exit_section_ref_ok(const char *name)
  1090. {
  1091. const char **s;
  1092. /* Absolute section names */
  1093. const char *namelist1[] = {
  1094. ".exit.data",
  1095. ".exit.text",
  1096. ".exitcall.exit",
  1097. ".rodata",
  1098. NULL
  1099. };
  1100. if (initexit_section_ref_ok(name))
  1101. return 1;
  1102. for (s = namelist1; *s; s++)
  1103. if (strcmp(*s, name) == 0)
  1104. return 1;
  1105. return 0;
  1106. }
  1107. static void read_symbols(char *modname)
  1108. {
  1109. const char *symname;
  1110. char *version;
  1111. char *license;
  1112. struct module *mod;
  1113. struct elf_info info = { };
  1114. Elf_Sym *sym;
  1115. if (!parse_elf(&info, modname))
  1116. return;
  1117. mod = new_module(modname);
  1118. /* When there's no vmlinux, don't print warnings about
  1119. * unresolved symbols (since there'll be too many ;) */
  1120. if (is_vmlinux(modname)) {
  1121. have_vmlinux = 1;
  1122. mod->skip = 1;
  1123. }
  1124. license = get_modinfo(info.modinfo, info.modinfo_len, "license");
  1125. while (license) {
  1126. if (license_is_gpl_compatible(license))
  1127. mod->gpl_compatible = 1;
  1128. else {
  1129. mod->gpl_compatible = 0;
  1130. break;
  1131. }
  1132. license = get_next_modinfo(info.modinfo, info.modinfo_len,
  1133. "license", license);
  1134. }
  1135. for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
  1136. symname = info.strtab + sym->st_name;
  1137. handle_modversions(mod, &info, sym, symname);
  1138. handle_moddevtable(mod, &info, sym, symname);
  1139. }
  1140. if (is_vmlinux(modname) && vmlinux_section_warnings) {
  1141. check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
  1142. check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
  1143. }
  1144. version = get_modinfo(info.modinfo, info.modinfo_len, "version");
  1145. if (version)
  1146. maybe_frob_rcs_version(modname, version, info.modinfo,
  1147. version - (char *)info.hdr);
  1148. if (version || (all_versions && !is_vmlinux(modname)))
  1149. get_src_version(modname, mod->srcversion,
  1150. sizeof(mod->srcversion)-1);
  1151. parse_elf_finish(&info);
  1152. /* Our trick to get versioning for struct_module - it's
  1153. * never passed as an argument to an exported function, so
  1154. * the automatic versioning doesn't pick it up, but it's really
  1155. * important anyhow */
  1156. if (modversions)
  1157. mod->unres = alloc_symbol("struct_module", 0, mod->unres);
  1158. }
  1159. #define SZ 500
  1160. /* We first write the generated file into memory using the
  1161. * following helper, then compare to the file on disk and
  1162. * only update the later if anything changed */
  1163. void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
  1164. const char *fmt, ...)
  1165. {
  1166. char tmp[SZ];
  1167. int len;
  1168. va_list ap;
  1169. va_start(ap, fmt);
  1170. len = vsnprintf(tmp, SZ, fmt, ap);
  1171. buf_write(buf, tmp, len);
  1172. va_end(ap);
  1173. }
  1174. void buf_write(struct buffer *buf, const char *s, int len)
  1175. {
  1176. if (buf->size - buf->pos < len) {
  1177. buf->size += len + SZ;
  1178. buf->p = realloc(buf->p, buf->size);
  1179. }
  1180. strncpy(buf->p + buf->pos, s, len);
  1181. buf->pos += len;
  1182. }
  1183. static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
  1184. {
  1185. const char *e = is_vmlinux(m) ?"":".ko";
  1186. switch (exp) {
  1187. case export_gpl:
  1188. fatal("modpost: GPL-incompatible module %s%s "
  1189. "uses GPL-only symbol '%s'\n", m, e, s);
  1190. break;
  1191. case export_unused_gpl:
  1192. fatal("modpost: GPL-incompatible module %s%s "
  1193. "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
  1194. break;
  1195. case export_gpl_future:
  1196. warn("modpost: GPL-incompatible module %s%s "
  1197. "uses future GPL-only symbol '%s'\n", m, e, s);
  1198. break;
  1199. case export_plain:
  1200. case export_unused:
  1201. case export_unknown:
  1202. /* ignore */
  1203. break;
  1204. }
  1205. }
  1206. static void check_for_unused(enum export exp, const char* m, const char* s)
  1207. {
  1208. const char *e = is_vmlinux(m) ?"":".ko";
  1209. switch (exp) {
  1210. case export_unused:
  1211. case export_unused_gpl:
  1212. warn("modpost: module %s%s "
  1213. "uses symbol '%s' marked UNUSED\n", m, e, s);
  1214. break;
  1215. default:
  1216. /* ignore */
  1217. break;
  1218. }
  1219. }
  1220. static void check_exports(struct module *mod)
  1221. {
  1222. struct symbol *s, *exp;
  1223. for (s = mod->unres; s; s = s->next) {
  1224. const char *basename;
  1225. exp = find_symbol(s->name);
  1226. if (!exp || exp->module == mod)
  1227. continue;
  1228. basename = strrchr(mod->name, '/');
  1229. if (basename)
  1230. basename++;
  1231. else
  1232. basename = mod->name;
  1233. if (!mod->gpl_compatible)
  1234. check_for_gpl_usage(exp->export, basename, exp->name);
  1235. check_for_unused(exp->export, basename, exp->name);
  1236. }
  1237. }
  1238. /**
  1239. * Header for the generated file
  1240. **/
  1241. static void add_header(struct buffer *b, struct module *mod)
  1242. {
  1243. buf_printf(b, "#include <linux/module.h>\n");
  1244. buf_printf(b, "#include <linux/vermagic.h>\n");
  1245. buf_printf(b, "#include <linux/compiler.h>\n");
  1246. buf_printf(b, "\n");
  1247. buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
  1248. buf_printf(b, "\n");
  1249. buf_printf(b, "struct module __this_module\n");
  1250. buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
  1251. buf_printf(b, " .name = KBUILD_MODNAME,\n");
  1252. if (mod->has_init)
  1253. buf_printf(b, " .init = init_module,\n");
  1254. if (mod->has_cleanup)
  1255. buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
  1256. " .exit = cleanup_module,\n"
  1257. "#endif\n");
  1258. buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
  1259. buf_printf(b, "};\n");
  1260. }
  1261. /**
  1262. * Record CRCs for unresolved symbols
  1263. **/
  1264. static int add_versions(struct buffer *b, struct module *mod)
  1265. {
  1266. struct symbol *s, *exp;
  1267. int err = 0;
  1268. for (s = mod->unres; s; s = s->next) {
  1269. exp = find_symbol(s->name);
  1270. if (!exp || exp->module == mod) {
  1271. if (have_vmlinux && !s->weak) {
  1272. if (warn_unresolved) {
  1273. warn("\"%s\" [%s.ko] undefined!\n",
  1274. s->name, mod->name);
  1275. } else {
  1276. merror("\"%s\" [%s.ko] undefined!\n",
  1277. s->name, mod->name);
  1278. err = 1;
  1279. }
  1280. }
  1281. continue;
  1282. }
  1283. s->module = exp->module;
  1284. s->crc_valid = exp->crc_valid;
  1285. s->crc = exp->crc;
  1286. }
  1287. if (!modversions)
  1288. return err;
  1289. buf_printf(b, "\n");
  1290. buf_printf(b, "static const struct modversion_info ____versions[]\n");
  1291. buf_printf(b, "__attribute_used__\n");
  1292. buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
  1293. for (s = mod->unres; s; s = s->next) {
  1294. if (!s->module) {
  1295. continue;
  1296. }
  1297. if (!s->crc_valid) {
  1298. warn("\"%s\" [%s.ko] has no CRC!\n",
  1299. s->name, mod->name);
  1300. continue;
  1301. }
  1302. buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
  1303. }
  1304. buf_printf(b, "};\n");
  1305. return err;
  1306. }
  1307. static void add_depends(struct buffer *b, struct module *mod,
  1308. struct module *modules)
  1309. {
  1310. struct symbol *s;
  1311. struct module *m;
  1312. int first = 1;
  1313. for (m = modules; m; m = m->next) {
  1314. m->seen = is_vmlinux(m->name);
  1315. }
  1316. buf_printf(b, "\n");
  1317. buf_printf(b, "static const char __module_depends[]\n");
  1318. buf_printf(b, "__attribute_used__\n");
  1319. buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
  1320. buf_printf(b, "\"depends=");
  1321. for (s = mod->unres; s; s = s->next) {
  1322. const char *p;
  1323. if (!s->module)
  1324. continue;
  1325. if (s->module->seen)
  1326. continue;
  1327. s->module->seen = 1;
  1328. if ((p = strrchr(s->module->name, '/')) != NULL)
  1329. p++;
  1330. else
  1331. p = s->module->name;
  1332. buf_printf(b, "%s%s", first ? "" : ",", p);
  1333. first = 0;
  1334. }
  1335. buf_printf(b, "\";\n");
  1336. }
  1337. static void add_srcversion(struct buffer *b, struct module *mod)
  1338. {
  1339. if (mod->srcversion[0]) {
  1340. buf_printf(b, "\n");
  1341. buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
  1342. mod->srcversion);
  1343. }
  1344. }
  1345. static void write_if_changed(struct buffer *b, const char *fname)
  1346. {
  1347. char *tmp;
  1348. FILE *file;
  1349. struct stat st;
  1350. file = fopen(fname, "r");
  1351. if (!file)
  1352. goto write;
  1353. if (fstat(fileno(file), &st) < 0)
  1354. goto close_write;
  1355. if (st.st_size != b->pos)
  1356. goto close_write;
  1357. tmp = NOFAIL(malloc(b->pos));
  1358. if (fread(tmp, 1, b->pos, file) != b->pos)
  1359. goto free_write;
  1360. if (memcmp(tmp, b->p, b->pos) != 0)
  1361. goto free_write;
  1362. free(tmp);
  1363. fclose(file);
  1364. return;
  1365. free_write:
  1366. free(tmp);
  1367. close_write:
  1368. fclose(file);
  1369. write:
  1370. file = fopen(fname, "w");
  1371. if (!file) {
  1372. perror(fname);
  1373. exit(1);
  1374. }
  1375. if (fwrite(b->p, 1, b->pos, file) != b->pos) {
  1376. perror(fname);
  1377. exit(1);
  1378. }
  1379. fclose(file);
  1380. }
  1381. /* parse Module.symvers file. line format:
  1382. * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
  1383. **/
  1384. static void read_dump(const char *fname, unsigned int kernel)
  1385. {
  1386. unsigned long size, pos = 0;
  1387. void *file = grab_file(fname, &size);
  1388. char *line;
  1389. if (!file)
  1390. /* No symbol versions, silently ignore */
  1391. return;
  1392. while ((line = get_next_line(&pos, file, size))) {
  1393. char *symname, *modname, *d, *export, *end;
  1394. unsigned int crc;
  1395. struct module *mod;
  1396. struct symbol *s;
  1397. if (!(symname = strchr(line, '\t')))
  1398. goto fail;
  1399. *symname++ = '\0';
  1400. if (!(modname = strchr(symname, '\t')))
  1401. goto fail;
  1402. *modname++ = '\0';
  1403. if ((export = strchr(modname, '\t')) != NULL)
  1404. *export++ = '\0';
  1405. if (export && ((end = strchr(export, '\t')) != NULL))
  1406. *end = '\0';
  1407. crc = strtoul(line, &d, 16);
  1408. if (*symname == '\0' || *modname == '\0' || *d != '\0')
  1409. goto fail;
  1410. if (!(mod = find_module(modname))) {
  1411. if (is_vmlinux(modname)) {
  1412. have_vmlinux = 1;
  1413. }
  1414. mod = new_module(NOFAIL(strdup(modname)));
  1415. mod->skip = 1;
  1416. }
  1417. s = sym_add_exported(symname, mod, export_no(export));
  1418. s->kernel = kernel;
  1419. s->preloaded = 1;
  1420. sym_update_crc(symname, mod, crc, export_no(export));
  1421. }
  1422. return;
  1423. fail:
  1424. fatal("parse error in symbol dump file\n");
  1425. }
  1426. /* For normal builds always dump all symbols.
  1427. * For external modules only dump symbols
  1428. * that are not read from kernel Module.symvers.
  1429. **/
  1430. static int dump_sym(struct symbol *sym)
  1431. {
  1432. if (!external_module)
  1433. return 1;
  1434. if (sym->vmlinux || sym->kernel)
  1435. return 0;
  1436. return 1;
  1437. }
  1438. static void write_dump(const char *fname)
  1439. {
  1440. struct buffer buf = { };
  1441. struct symbol *symbol;
  1442. int n;
  1443. for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
  1444. symbol = symbolhash[n];
  1445. while (symbol) {
  1446. if (dump_sym(symbol))
  1447. buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
  1448. symbol->crc, symbol->name,
  1449. symbol->module->name,
  1450. export_str(symbol->export));
  1451. symbol = symbol->next;
  1452. }
  1453. }
  1454. write_if_changed(&buf, fname);
  1455. }
  1456. int main(int argc, char **argv)
  1457. {
  1458. struct module *mod;
  1459. struct buffer buf = { };
  1460. char fname[SZ];
  1461. char *kernel_read = NULL, *module_read = NULL;
  1462. char *dump_write = NULL;
  1463. int opt;
  1464. int err;
  1465. while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
  1466. switch(opt) {
  1467. case 'i':
  1468. kernel_read = optarg;
  1469. break;
  1470. case 'I':
  1471. module_read = optarg;
  1472. external_module = 1;
  1473. break;
  1474. case 'm':
  1475. modversions = 1;
  1476. break;
  1477. case 'o':
  1478. dump_write = optarg;
  1479. break;
  1480. case 'a':
  1481. all_versions = 1;
  1482. break;
  1483. case 's':
  1484. vmlinux_section_warnings = 0;
  1485. break;
  1486. case 'w':
  1487. warn_unresolved = 1;
  1488. break;
  1489. default:
  1490. exit(1);
  1491. }
  1492. }
  1493. if (kernel_read)
  1494. read_dump(kernel_read, 1);
  1495. if (module_read)
  1496. read_dump(module_read, 0);
  1497. while (optind < argc) {
  1498. read_symbols(argv[optind++]);
  1499. }
  1500. for (mod = modules; mod; mod = mod->next) {
  1501. if (mod->skip)
  1502. continue;
  1503. check_exports(mod);
  1504. }
  1505. err = 0;
  1506. for (mod = modules; mod; mod = mod->next) {
  1507. if (mod->skip)
  1508. continue;
  1509. buf.pos = 0;
  1510. add_header(&buf, mod);
  1511. err |= add_versions(&buf, mod);
  1512. add_depends(&buf, mod, modules);
  1513. add_moddevtable(&buf, mod);
  1514. add_srcversion(&buf, mod);
  1515. sprintf(fname, "%s.mod.c", mod->name);
  1516. write_if_changed(&buf, fname);
  1517. }
  1518. if (dump_write)
  1519. write_dump(dump_write);
  1520. return err;
  1521. }