modpost.c 43 KB

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