-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsmod.c
More file actions
1657 lines (1378 loc) · 36.8 KB
/
insmod.c
File metadata and controls
1657 lines (1378 loc) · 36.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Insert a module into a running kernel.
Copyright 1996, 1997 Linux International.
New implementation contributed by Richard Henderson <rth@tamu.edu>
Based on original work by Bjorn Eckwall <bj0rn@blox.se>
This file is part of the Linux modutils.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ident "$Id: insmod.c,v 1.2 1999/09/11 15:43:57 msw Exp $"
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <alloca.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <stddef.h>
#include <getopt.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "module.h"
#include "obj.h"
#include "util.h"
#include "version.h"
#include "logger.h"
#define STRVERSIONLEN 32
/*======================================================================*/
static int flag_force_load = 0;
static int flag_autoclean = 0;
static int flag_silent_poll = 0;
static int flag_verbose = 0;
static int flag_export = 1;
static int flag_load_map = 0;
struct external_module
{
const char *name;
ElfW(Addr) addr;
int used;
size_t nsyms;
struct new_module_symbol *syms;
};
static struct new_module_symbol *ksyms;
static size_t nksyms;
static struct external_module *ext_modules;
static int n_ext_modules;
static int n_ext_modules_used;
/*======================================================================*/
/* Given a bare module name, poke through the module path to find the file. */
static char *
search_module_path(char *base)
{
static const char default_path[] =
".:"
"/linux/modules:"
"/lib/modules/%s/fs:"
"/lib/modules/%s/net:"
"/lib/modules/%s/scsi:"
"/lib/modules/%s/block:"
"/lib/modules/%s/cdrom:"
"/lib/modules/%s/ipv4:"
"/lib/modules/%s/ipv6:"
"/lib/modules/%s/sound:"
"/lib/modules/%s/fc4:"
"/lib/modules/%s/video:"
"/lib/modules/%s/misc:"
"/lib/modules/default/fs:"
"/lib/modules/default/net:"
"/lib/modules/default/scsi:"
"/lib/modules/default/block:"
"/lib/modules/default/cdrom:"
"/lib/modules/default/ipv4:"
"/lib/modules/default/ipv6:"
"/lib/modules/default/sound:"
"/lib/modules/default/fc4:"
"/lib/modules/default/video:"
"/lib/modules/default/misc:"
"/lib/modules/fs:"
"/lib/modules/net:"
"/lib/modules/scsi:"
"/lib/modules/block:"
"/lib/modules/cdrom:"
"/lib/modules/ipv4:"
"/lib/modules/ipv6:"
"/lib/modules/sound:"
"/lib/modules/fc4:"
"/lib/modules/video:"
"/lib/modules/misc";
char *path, *p, *filename;
struct utsname uts_info;
size_t len;
if ((path = getenv("MODPATH")) == NULL)
path = (char *)default_path;
/* Make a copy so's we can mung it with strtok. */
len = strlen(path)+1;
p = alloca(len);
path = memcpy(p, path, len);
uname(&uts_info);
filename = xmalloc(PATH_MAX);
for (p = strtok(path, ":"); p != NULL ; p = strtok(NULL, ":"))
{
struct stat sb;
len = snprintf(filename, PATH_MAX, p, uts_info.release);
len += snprintf(filename+len, PATH_MAX-len, "/%s", base);
if (stat(filename, &sb) == 0 && S_ISREG(sb.st_mode))
return filename;
snprintf(filename+len, PATH_MAX-len, ".o");
if (stat(filename, &sb) == 0 && S_ISREG(sb.st_mode))
return filename;
}
free(filename);
return NULL;
}
/* Get the kernel version in the canonical integer form. */
static int
get_kernel_version(char str[STRVERSIONLEN])
{
struct utsname uts_info;
char *p, *q;
int a, b, c;
if (uname(&uts_info) < 0)
return -1;
strncpy(str, uts_info.release, STRVERSIONLEN);
p = uts_info.release;
a = strtoul(p, &p, 10);
if (*p != '.')
return -1;
b = strtoul(p+1, &p, 10);
if (*p != '.')
return -1;
c = strtoul(p+1, &q, 10);
if (p+1 == q)
return -1;
return a << 16 | b << 8 | c;
}
/* String comparison for non-co-versioned kernel and module. */
static int
ncv_strcmp(const char *a, const char *b)
{
size_t alen = strlen(a), blen = strlen(b);
if (blen == alen + 10 && b[alen] == '_' && b[alen+1] == 'R')
return strncmp(a, b, alen);
else if (alen == blen + 10 && a[blen] == '_' && a[blen+1] == 'R')
return strncmp(a, b, blen);
else
return strcmp(a, b);
}
/* String hashing for non-co-versioned kernel and module. Here
we are simply forced to drop the crc from the hash. */
static unsigned long
ncv_symbol_hash(const char *str)
{
size_t len = strlen(str);
if (len > 10 && str[len-10] == '_' && str[len-9] == 'R')
len -= 10;
return obj_elf_hash_n(str, len);
}
/* Conditionally add the symbols from the given symbol set to the
new module. */
static int
add_symbols_from(struct obj_file *f, int idx,
struct new_module_symbol *syms, size_t nsyms)
{
struct new_module_symbol *s;
size_t i;
int used = 0;
for (i = 0, s = syms; i < nsyms; ++i, ++s)
{
/* Only add symbols that are already marked external. If we
override locals we may cause problems for argument initialization.
We will also create a false dependency on the module. */
struct obj_symbol *sym;
sym = obj_find_symbol(f, (char *)s->name);
if (sym && ! ELFW(ST_BIND)(sym->info) == STB_LOCAL)
{
sym = obj_add_symbol(f, (char *)s->name, -1,
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE),
idx, s->value, 0);
/* Did our symbol just get installed? If so, mark the
module as "used". */
if (sym->secidx == idx)
used = 1;
}
}
return used;
}
static void
add_kernel_symbols(struct obj_file *f)
{
struct external_module *m;
size_t i, nused = 0;
/* Add module symbols first. */
for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
if (m->nsyms && add_symbols_from(f, SHN_HIRESERVE+2+i, m->syms, m->nsyms))
m->used = 1, ++nused;
n_ext_modules_used = nused;
/* And finally the symbols from the kernel proper. */
if (nksyms)
add_symbols_from(f, SHN_HIRESERVE+1, ksyms, nksyms);
}
static void
hide_special_symbols(struct obj_file *f)
{
static const char * const specials[] =
{
"cleanup_module",
"init_module",
"kernel_version",
NULL
};
struct obj_symbol *sym;
const char * const *p;
for (p = specials; *p ; ++p)
if ((sym = obj_find_symbol(f, *p)) != NULL)
sym->info = ELFW(ST_INFO)(STB_LOCAL, ELFW(ST_TYPE)(sym->info));
}
static void
print_load_map(struct obj_file *f)
{
int
load_map_cmp(const void *a, const void *b)
{
struct obj_symbol **as = (struct obj_symbol **)a;
struct obj_symbol **bs = (struct obj_symbol **)b;
unsigned long aa = obj_symbol_final_value(f, *as);
unsigned long ba = obj_symbol_final_value(f, *bs);
return aa < ba ? -1 : aa > ba ? 1 : 0;
}
int i, nsyms, *loaded;
struct obj_symbol *sym;
struct obj_symbol **all, **p;
struct obj_section *sec;
/* Report on the section layout. */
lprintf("Sections: Size %-*s Align",
(int)(2*sizeof(void*)), "Address");
for (sec = f->load_order; sec ; sec = sec->load_next)
{
int a;
unsigned long tmp;
for (a = -1, tmp = sec->header.sh_addralign; tmp ; ++a)
tmp >>= 1;
if (a == -1)
a = 0;
lprintf("%-16s%08lx %0*lx 2**%d", sec->name, sec->header.sh_size,
(int)(2*sizeof(void*)), sec->header.sh_addr, a);
}
/* Quick reference which section indicies are loaded. */
loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
while (--i >= 0)
loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
/* Collect the symbols we'll be listing. */
for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
for (sym = f->symtab[i]; sym; sym = sym->next)
if (sym->secidx <= SHN_HIRESERVE
&& (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
++nsyms;
all = alloca(nsyms * sizeof(struct obj_symbol *));
for (i = 0, p = all; i < HASH_BUCKETS; ++i)
for (sym = f->symtab[i]; sym; sym = sym->next)
if (sym->secidx <= SHN_HIRESERVE
&& (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
*p++ = sym;
/* Sort them by final value. */
qsort(all, nsyms, sizeof(struct obj_file *), load_map_cmp);
/* And list them. */
lprintf("\nSymbols:");
for (p = all; p < all+nsyms; ++p)
{
char type = '?';
unsigned long value;
sym = *p;
if (sym->secidx == SHN_ABS)
{
type = 'A';
value = sym->value;
}
else if (sym->secidx == SHN_UNDEF)
{
type = 'U';
value = 0;
}
else
{
struct obj_section *sec = f->sections[sym->secidx];
if (sec->header.sh_type == SHT_NOBITS)
type = 'B';
else if (sec->header.sh_flags & SHF_ALLOC)
{
if (sec->header.sh_flags & SHF_EXECINSTR)
type = 'T';
else if (sec->header.sh_flags & SHF_WRITE)
type = 'D';
else
type = 'R';
}
value = sym->value + sec->header.sh_addr;
}
if (ELFW(ST_BIND)(sym->info) == STB_LOCAL)
type = tolower(type);
lprintf("%0*lx %c %s", (int)(2*sizeof(void*)), value,
type, sym->name);
}
}
/*======================================================================*/
/* Functions relating to module loading in pre 2.1 kernels. */
/* Fetch all the symbols and divvy them up as appropriate for the modules. */
static int
old_get_kernel_symbols(void)
{
struct old_kernel_sym *ks, *k;
struct new_module_symbol *s;
struct external_module *mod;
int nks, nms, nmod, i;
nks = get_kernel_syms(NULL);
if (nks < 0)
{
error("get_kernel_syms: %m");
return 0;
}
ks = k = xmalloc(nks * sizeof(*ks));
if (get_kernel_syms(ks) != nks)
{
error("inconsistency with get_kernel_syms -- is someone else "
"playing with modules?");
free(ks);
return 0;
}
/* Collect the module information. */
mod = NULL;
nmod = -1;
while (k->name[0] == '#' && k->name[1])
{
struct old_kernel_sym *k2;
struct new_module_symbol *s;
/* Find out how many symbols this module has. */
for (k2 = k+1; k2->name[0] != '#'; ++k2)
continue;
nms = k2 - k - 1;
mod = xrealloc(mod, (++nmod+1) * sizeof(*mod));
mod[nmod].name = k->name+1;
mod[nmod].addr = k->value;
mod[nmod].used = 0;
mod[nmod].nsyms = nms;
mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
for (i = 0, ++k; i < nms; ++i, ++s, ++k)
{
s->name = (unsigned long)k->name;
s->value = k->value;
}
k = k2;
}
ext_modules = mod;
n_ext_modules = nmod+1;
/* Now collect the symbols for the kernel proper. */
if (k->name[0] == '#')
++k;
nksyms = nms = nks - (k - ks);
ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
for (i = 0; i < nms; ++i, ++s, ++k)
{
s->name = (unsigned long)k->name;
s->value = k->value;
}
return 1;
}
/* Return the kernel symbol checksum version, or zero if not used. */
static int
old_is_kernel_checksummed(void)
{
/* Using_Versions is the first symbol. */
if (nksyms > 0 && strcmp((char *)ksyms[0].name, "Using_Versions") == 0)
return ksyms[0].value;
else
return 0;
}
/* Get the module's kernel version in the canonical integer form. */
static int
old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
{
struct obj_symbol *sym;
char *p, *q;
int a, b, c;
sym = obj_find_symbol(f, "kernel_version");
if (sym == NULL)
return -1;
p = f->sections[sym->secidx]->contents + sym->value;
strncpy(str, p, STRVERSIONLEN);
a = strtoul(p, &p, 10);
if (*p != '.')
return -1;
b = strtoul(p+1, &p, 10);
if (*p != '.')
return -1;
c = strtoul(p+1, &q, 10);
if (p+1 == q)
return -1;
return a << 16 | b << 8 | c;
}
static int
old_is_module_checksummed(struct obj_file *f)
{
return obj_find_symbol(f, "Using_Versions") != NULL;
}
static int
old_create_mod_use_count(struct obj_file *f)
{
struct obj_section *sec;
sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
sizeof(long));
obj_add_symbol(f, "mod_use_count_", -1, ELFW(ST_INFO)(STB_LOCAL, STT_OBJECT),
sec->idx, 0, sizeof(long));
return 1;
}
static int
old_process_module_arguments(struct obj_file *f, int argc, char **argv)
{
while (argc > 0)
{
char *p, *q;
struct obj_symbol *sym;
int *loc;
p = *argv;
if ((q = strchr(p, '=')) == NULL)
continue;
*q++ = '\0';
sym = obj_find_symbol(f, p);
/* Also check that the parameter was not resolved from the kernel. */
if (sym == NULL || sym->secidx > SHN_HIRESERVE)
{
error("symbol for parameter %s not found", p);
return 0;
}
loc = (int *)(f->sections[sym->secidx]->contents + sym->value);
/* Do C quoting if we begin with a ". */
if (*q == '"')
{
char *r, *str;
str = alloca(strlen(q));
for (r = str, q++; *q != '"'; ++q, ++r)
{
if (*q == '\0')
{
error("improperly terminated string argument for %s", p);
return 0;
}
else if (*q == '\\')
switch (*++q)
{
case 'a': *r = '\a'; break;
case 'b': *r = '\b'; break;
case 'e': *r = '\033'; break;
case 'f': *r = '\f'; break;
case 'n': *r = '\n'; break;
case 'r': *r = '\r'; break;
case 't': *r = '\t'; break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
{
int c = *q - '0';
if (q[1] >= '0' && q[1] <= '7')
{
c = (c * 8) + *++q - '0';
if (q[1] >= '0' && q[1] <= '7')
c = (c * 8) + *++q - '0';
}
*r = c;
}
break;
default:
*r = *q;
break;
}
else
*r = *q;
}
*r = '\0';
obj_string_patch(f, sym->secidx, sym->value, str);
}
else if (*q >= '0' && *q <= '9')
{
do
*loc++ = strtoul(q, &q, 0);
while (*q++ == ',');
}
else
{
char *contents = f->sections[sym->secidx]->contents;
char *loc = contents + sym->value;
char *r; /* To search for commas */
/* Break the string with comas */
while((r = strchr(q, ',')) != (char *) NULL)
{
*r++ = '\0';
obj_string_patch(f, sym->secidx, loc-contents, q);
loc += sizeof(char*);
q = r;
}
/* last part */
obj_string_patch(f, sym->secidx, loc-contents, q);
}
argc--, argv++;
}
return 1;
}
static int
old_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
{
char *image;
struct old_mod_routines routines;
struct old_symbol_table *symtab;
int ret;
/* Create the symbol table */
{
int nsyms = 0, strsize = 0, total;
/* Size things first... */
if (flag_export)
{
int i;
for (i = 0; i < HASH_BUCKETS; ++i)
{
struct obj_symbol *sym;
for (sym = f->symtab[i]; sym; sym = sym->next)
if (ELFW(ST_BIND)(sym->info) != STB_LOCAL
&& sym->secidx <= SHN_HIRESERVE)
{
sym->ksymidx = nsyms++;
strsize += strlen(sym->name)+1;
}
}
}
total = (sizeof(struct old_symbol_table)
+ nsyms * sizeof(struct old_module_symbol)
+ n_ext_modules_used * sizeof(struct old_module_ref)
+ strsize);
symtab = xmalloc(total);
symtab->size = total;
symtab->n_symbols = nsyms;
symtab->n_refs = n_ext_modules_used;
if (flag_export && nsyms)
{
struct old_module_symbol *ksym;
char *str;
int i;
ksym = symtab->symbol;
str = ((char *)ksym
+ nsyms * sizeof(struct old_module_symbol)
+ n_ext_modules_used * sizeof(struct old_module_ref));
for (i = 0; i < HASH_BUCKETS; ++i)
{
struct obj_symbol *sym;
for (sym = f->symtab[i]; sym; sym = sym->next)
if (sym->ksymidx >= 0)
{
ksym->addr = obj_symbol_final_value(f, sym);
ksym->name = (unsigned long)str - (unsigned long)symtab;
str = stpcpy(str, sym->name)+1;
ksym++;
}
}
}
if (n_ext_modules_used)
{
struct old_module_ref *ref;
int i;
ref = (struct old_module_ref *)
((char *)symtab->symbol + nsyms * sizeof(struct old_module_symbol));
for (i = 0; i < n_ext_modules; ++i)
if (ext_modules[i].used)
ref++->module = ext_modules[i].addr;
}
}
/* Fill in routines. */
routines.init = obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
routines.cleanup
= obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
/* Whew! All of the initialization is complete. Collect the final
module image and give it to the kernel. */
image = xmalloc(m_size);
obj_create_image(f, image);
/* image holds the complete relocated module, accounting correctly for
mod_use_count. However the old module kernel support assume that
it is receiving something which does not contain mod_use_count. */
ret = old_sys_init_module(m_name, image+sizeof(long),
m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN : 0),
&routines, symtab);
if (ret)
error("init_module: %m");
free(image);
free(symtab);
return ret == 0;
}
/*======================================================================*/
/* Functions relating to module loading after 2.1.18. */
/* Fetch the loaded modules, and all currently exported symbols. */
static int
new_get_kernel_symbols(void)
{
char *module_names, *mn;
struct external_module *modules, *m;
struct new_module_symbol *syms, *s;
size_t ret, bufsize, nmod, nsyms, i, j;
/* Collect the loaded modules. */
module_names = xmalloc(bufsize = 256);
retry_modules_load:
if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret))
{
if (errno == ENOSPC)
{
module_names = xrealloc(module_names, bufsize = ret);
goto retry_modules_load;
}
error("QM_MODULES: %m\n");
return 0;
}
n_ext_modules = nmod = ret;
ext_modules = modules = xmalloc(nmod * sizeof(*modules));
memset(modules, 0, nmod * sizeof(*modules));
/* Collect the modules' symbols. */
for (i = 0, mn = module_names, m = modules;
i < nmod;
++i, ++m, mn += strlen(mn)+1)
{
struct new_module_info info;
if (query_module(mn, QM_INFO, &info, sizeof(info), &ret))
{
if (errno == ENOENT)
/* The module was removed out from underneath us. */
continue;
error("module %s: QM_INFO: %m", mn);
return 0;
}
syms = xmalloc(bufsize = 1024);
retry_mod_sym_load:
if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret))
{
switch (errno)
{
case ENOSPC:
syms = xrealloc(syms, bufsize = ret);
goto retry_mod_sym_load;
case ENOENT:
/* The module was removed out from underneath us. */
continue;
default:
error("module %s: QM_SYMBOLS: %m", mn);
return 0;
}
}
nsyms = ret;
m->name = mn;
m->addr = info.addr;
m->nsyms = nsyms;
m->syms = syms;
for (j = 0, s = syms; j < nsyms; ++j, ++s)
s->name += (unsigned long)syms;
}
/* Collect the kernel's symbols. */
syms = xmalloc(bufsize = 16*1024);
retry_kern_sym_load:
if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret))
{
if (errno == ENOSPC)
{
syms = xrealloc(syms, bufsize = ret);
goto retry_kern_sym_load;
}
error("kernel: QM_SYMBOLS: %m");
return 0;
}
nksyms = nsyms = ret;
ksyms = syms;
for (j = 0, s = syms; j < nsyms; ++j, ++s)
s->name += (unsigned long)syms;
return 1;
}
/* Return the kernel symbol checksum version, or zero if not used. */
static int
new_is_kernel_checksummed(void)
{
struct new_module_symbol *s;
size_t i;
/* Using_Versions is not the first symbol, but it should be in there. */
for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
if (strcmp((char *)s->name, "Using_Versions") == 0)
return s->value;
return 0;
}
static char *
get_modinfo_value(struct obj_file *f, const char *key)
{
struct obj_section *sec;
char *p, *v, *n, *ep;
size_t klen = strlen(key);
sec = obj_find_section(f, ".modinfo");
if (sec == NULL)
return NULL;
p = sec->contents;
ep = p + sec->header.sh_size;
while (p < ep)
{
v = strchr(p, '=');
n = strchr(p, '\0');
if (v)
{
if (v-p == klen && strncmp(p, key, klen) == 0)
return v+1;
}
else
{
if (n-p == klen && strcmp(p, key) == 0)
return n;
}
p = n+1;
}
return NULL;
}
/* Get the module's kernel version in the canonical integer form. */
static int
new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
{
char *p, *q;
int a, b, c;
p = get_modinfo_value(f, "kernel_version");
if (p == NULL)
return -1;
strncpy(str, p, STRVERSIONLEN);
a = strtoul(p, &p, 10);
if (*p != '.')
return -1;
b = strtoul(p+1, &p, 10);
if (*p != '.')
return -1;
c = strtoul(p+1, &q, 10);
if (p+1 == q)
return -1;
return a << 16 | b << 8 | c;
}
static int
new_is_module_checksummed(struct obj_file *f)
{
const char *p = get_modinfo_value(f, "using_checksums");
if (p)
return atoi(p);
else
return 0;
}
static int
new_process_module_arguments(struct obj_file *f, int argc, char **argv)
{
while (argc > 0)
{
char *p, *q, *key;
struct obj_symbol *sym;
char *contents, *loc;
int min, max, n;
p = *argv;
if ((q = strchr(p, '=')) == NULL)
continue;
key = alloca(q-p + 6);
memcpy(key, "parm_", 5);
memcpy(key+5, p, q-p);
key[q-p+5] = 0;
p = get_modinfo_value(f, key);
key += 5;
if (p == NULL)
{
error("invalid parameter %s", key);
return 0;
}
sym = obj_find_symbol(f, key);
/* Also check that the parameter was not resolved from the kernel. */
if (sym == NULL || sym->secidx > SHN_HIRESERVE)
{
error("symbol for parameter %s not found", key);
return 0;
}
if (isdigit(*p))
{
min = strtoul(p, &p, 10);
if (*p == '-')
max = strtoul(p+1, &p, 10);
else
max = min;
}
else
min = max = 1;
contents = f->sections[sym->secidx]->contents;
loc = contents + sym->value;
n = (*++q != '\0');
while (1)
{
if((*p == 's') || (*p == 'c'))
{
char *str;
/* Do C quoting if we begin with a ", else slurp the lot. */
if (*q == '"')
{
char *r;
str = alloca(strlen(q));
for (r = str, q++; *q != '"'; ++q, ++r)
{
if (*q == '\0')
{
error("improperly terminated string argument for %s",
key);
return 0;
}
else if (*q == '\\')
switch (*++q)
{
case 'a': *r = '\a'; break;