modpost.c 42 KB

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