modpost.c 43 KB

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