Patch from Robert Griebl <griebl@gmx.de> to ignore the -s flag
[oweals/busybox.git] / modutils / insmod.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini insmod implementation for busybox
4  *
5  * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k, 
6  * MIPS, and v850e.
7  *
8  * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
9  * Copyright (C) 1999,2000,2001,2002 by Erik Andersen <andersee@debian.org>
10  * and Ron Alder <alder@lineo.com>
11  *
12  * Miles Bader <miles@gnu.org> added NEC V850E support.
13  *
14  * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
15  * and (theoretically) SH3. I have only tested SH4 in little endian mode.
16  *
17  * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
18  * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI.  Only
19  * very minor changes required to also work with StrongArm and presumably
20  * all ARM based systems.
21  *
22  * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
23  *   PowerPC specific code stolen from modutils-2.3.16, 
24  *   written by Paul Mackerras, Copyright 1996, 1997 Linux International.
25  *   I've only tested the code on mpc8xx platforms in big-endian mode.
26  *   Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
27  *
28  * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
29  *   based on modutils-2.4.2
30  *   MIPS specific support for Elf loading and relocation.
31  *   Copyright 1996, 1997 Linux International.
32  *   Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
33  *
34  * Based almost entirely on the Linux modutils-2.3.11 implementation.
35  *   Copyright 1996, 1997 Linux International.
36  *   New implementation contributed by Richard Henderson <rth@tamu.edu>
37  *   Based on original work by Bjorn Ekwall <bj0rn@blox.se>
38  *   Restructured (and partly rewritten) by:
39  *   Björn Ekwall <bj0rn@blox.se> February 1999
40  *
41  * This program is free software; you can redistribute it and/or modify
42  * it under the terms of the GNU General Public License as published by
43  * the Free Software Foundation; either version 2 of the License, or
44  * (at your option) any later version.
45  *
46  * This program is distributed in the hope that it will be useful,
47  * but WITHOUT ANY WARRANTY; without even the implied warranty of
48  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49  * General Public License for more details.
50  *
51  * You should have received a copy of the GNU General Public License
52  * along with this program; if not, write to the Free Software
53  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54  *
55  */
56
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <stddef.h>
60 #include <errno.h>
61 #include <unistd.h>
62 #include <dirent.h>
63 #include <ctype.h>
64 #include <assert.h>
65 #include <string.h>
66 #include <getopt.h>
67 #include <sys/utsname.h>
68 #include "busybox.h"
69
70 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
71 # undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
72 # define new_sys_init_module    init_module
73 #else
74 # define old_sys_init_module    init_module
75 #endif
76
77 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
78 #define LOADBITS 0      
79 #else
80 #define LOADBITS 1
81 #endif
82
83 #if defined(__powerpc__)
84 #define CONFIG_USE_PLT_ENTRIES
85 #define CONFIG_PLT_ENTRY_SIZE 16
86 #endif
87
88 #if defined(__arm__)
89 #define CONFIG_USE_PLT_ENTRIES
90 #define CONFIG_PLT_ENTRY_SIZE 8
91 #define CONFIG_USE_GOT_ENTRIES
92 #define CONFIG_GOT_ENTRY_SIZE 8
93 #endif
94
95 #if defined(__sh__)
96 #define CONFIG_USE_GOT_ENTRIES
97 #define CONFIG_GOT_ENTRY_SIZE 4
98 #endif
99
100 #if defined(__i386__)
101 #define CONFIG_USE_GOT_ENTRIES
102 #define CONFIG_GOT_ENTRY_SIZE 4
103 #endif
104
105 #if defined(__mips__)
106 // neither used
107 #endif
108
109 #if defined (__v850e__)
110 #define CONFIG_USE_PLT_ENTRIES
111 #define CONFIG_PLT_ENTRY_SIZE 8
112 #endif
113
114 //----------------------------------------------------------------------------
115 //--------modutils module.h, lines 45-242
116 //----------------------------------------------------------------------------
117
118 /* Definitions for the Linux module syscall interface.
119    Copyright 1996, 1997 Linux International.
120
121    Contributed by Richard Henderson <rth@tamu.edu>
122
123    This file is part of the Linux modutils.
124
125    This program is free software; you can redistribute it and/or modify it
126    under the terms of the GNU General Public License as published by the
127    Free Software Foundation; either version 2 of the License, or (at your
128    option) any later version.
129
130    This program is distributed in the hope that it will be useful, but
131    WITHOUT ANY WARRANTY; without even the implied warranty of
132    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
133    General Public License for more details.
134
135    You should have received a copy of the GNU General Public License
136    along with this program; if not, write to the Free Software Foundation,
137    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
138
139
140 #ifndef MODUTILS_MODULE_H
141 static const int MODUTILS_MODULE_H = 1;
142
143 #ident "$Id: insmod.c,v 1.82 2002/05/03 10:34:35 andersen Exp $"
144
145 /* This file contains the structures used by the 2.0 and 2.1 kernels.
146    We do not use the kernel headers directly because we do not wish
147    to be dependant on a particular kernel version to compile insmod.  */
148
149
150 /*======================================================================*/
151 /* The structures used by Linux 2.0.  */
152
153 /* The symbol format used by get_kernel_syms(2).  */
154 struct old_kernel_sym
155 {
156   unsigned long value;
157   char name[60];
158 };
159
160 struct old_module_ref
161 {
162   unsigned long module;         /* kernel addresses */
163   unsigned long next;
164 };
165
166 struct old_module_symbol
167 {
168   unsigned long addr;
169   unsigned long name;
170 };
171
172 struct old_symbol_table
173 {
174   int size;                     /* total, including string table!!! */
175   int n_symbols;
176   int n_refs;
177   struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
178   struct old_module_ref ref[0]; /* actual size defined by n_refs */
179 };
180
181 struct old_mod_routines
182 {
183   unsigned long init;
184   unsigned long cleanup;
185 };
186
187 struct old_module
188 {
189   unsigned long next;
190   unsigned long ref;            /* the list of modules that refer to me */
191   unsigned long symtab;
192   unsigned long name;
193   int size;                     /* size of module in pages */
194   unsigned long addr;           /* address of module */
195   int state;
196   unsigned long cleanup;        /* cleanup routine */
197 };
198
199 /* Sent to init_module(2) or'ed into the code size parameter.  */
200 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
201
202 int get_kernel_syms(struct old_kernel_sym *);
203 int old_sys_init_module(const char *name, char *code, unsigned codesize,
204                         struct old_mod_routines *, struct old_symbol_table *);
205
206 /*======================================================================*/
207 /* For sizeof() which are related to the module platform and not to the
208    environment isnmod is running in, use sizeof_xx instead of sizeof(xx).  */
209
210 #define tgt_sizeof_char         sizeof(char)
211 #define tgt_sizeof_short        sizeof(short)
212 #define tgt_sizeof_int          sizeof(int)
213 #define tgt_sizeof_long         sizeof(long)
214 #define tgt_sizeof_char_p       sizeof(char *)
215 #define tgt_sizeof_void_p       sizeof(void *)
216 #define tgt_long                long
217
218 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
219 #undef tgt_sizeof_long
220 #undef tgt_sizeof_char_p
221 #undef tgt_sizeof_void_p
222 #undef tgt_long
223 static const int tgt_sizeof_long = 8;
224 static const int tgt_sizeof_char_p = 8;
225 static const int tgt_sizeof_void_p = 8;
226 #define tgt_long                long long
227 #endif
228
229 /*======================================================================*/
230 /* The structures used in Linux 2.1.  */
231
232 /* Note: new_module_symbol does not use tgt_long intentionally */
233 struct new_module_symbol
234 {
235   unsigned long value;
236   unsigned long name;
237 };
238
239 struct new_module_persist;
240
241 struct new_module_ref
242 {
243   unsigned tgt_long dep;                /* kernel addresses */
244   unsigned tgt_long ref;
245   unsigned tgt_long next_ref;
246 };
247
248 struct new_module
249 {
250   unsigned tgt_long size_of_struct;     /* == sizeof(module) */
251   unsigned tgt_long next;
252   unsigned tgt_long name;
253   unsigned tgt_long size;
254
255   tgt_long usecount;
256   unsigned tgt_long flags;              /* AUTOCLEAN et al */
257
258   unsigned nsyms;
259   unsigned ndeps;
260
261   unsigned tgt_long syms;
262   unsigned tgt_long deps;
263   unsigned tgt_long refs;
264   unsigned tgt_long init;
265   unsigned tgt_long cleanup;
266   unsigned tgt_long ex_table_start;
267   unsigned tgt_long ex_table_end;
268 #ifdef __alpha__
269   unsigned tgt_long gp;
270 #endif
271   /* Everything after here is extension.  */
272   unsigned tgt_long persist_start;
273   unsigned tgt_long persist_end;
274   unsigned tgt_long can_unload;
275   unsigned tgt_long runsize;
276 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
277   const char *kallsyms_start;     /* All symbols for kernel debugging */
278   const char *kallsyms_end;
279   const char *archdata_start;     /* arch specific data for module */
280   const char *archdata_end;
281   const char *kernel_data;        /* Reserved for kernel internal use */
282 #endif
283 };
284
285 #define ARCHDATA_SEC_NAME "__archdata"
286 #define KALLSYMS_SEC_NAME "__kallsyms"
287
288
289 struct new_module_info
290 {
291   unsigned long addr;
292   unsigned long size;
293   unsigned long flags;
294            long usecount;
295 };
296
297 /* Bits of module.flags.  */
298 static const int NEW_MOD_RUNNING = 1;
299 static const int NEW_MOD_DELETED = 2;
300 static const int NEW_MOD_AUTOCLEAN = 4;
301 static const int NEW_MOD_VISITED = 8;
302 static const int NEW_MOD_USED_ONCE = 16;
303
304 int new_sys_init_module(const char *name, const struct new_module *);
305 int query_module(const char *name, int which, void *buf, size_t bufsize,
306                  size_t *ret);
307
308 /* Values for query_module's which.  */
309
310 static const int QM_MODULES = 1;
311 static const int QM_DEPS = 2;
312 static const int QM_REFS = 3;
313 static const int QM_SYMBOLS = 4;
314 static const int QM_INFO = 5;
315
316 /*======================================================================*/
317 /* The system calls unchanged between 2.0 and 2.1.  */
318
319 unsigned long create_module(const char *, size_t);
320 int delete_module(const char *);
321
322
323 #endif /* module.h */
324
325 //----------------------------------------------------------------------------
326 //--------end of modutils module.h
327 //----------------------------------------------------------------------------
328
329
330
331 //----------------------------------------------------------------------------
332 //--------modutils obj.h, lines 253-462
333 //----------------------------------------------------------------------------
334
335 /* Elf object file loading and relocation routines.
336    Copyright 1996, 1997 Linux International.
337
338    Contributed by Richard Henderson <rth@tamu.edu>
339
340    This file is part of the Linux modutils.
341
342    This program is free software; you can redistribute it and/or modify it
343    under the terms of the GNU General Public License as published by the
344    Free Software Foundation; either version 2 of the License, or (at your
345    option) any later version.
346
347    This program is distributed in the hope that it will be useful, but
348    WITHOUT ANY WARRANTY; without even the implied warranty of
349    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
350    General Public License for more details.
351
352    You should have received a copy of the GNU General Public License
353    along with this program; if not, write to the Free Software Foundation,
354    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
355
356
357 #ifndef MODUTILS_OBJ_H
358 static const int MODUTILS_OBJ_H = 1;
359
360 #ident "$Id: insmod.c,v 1.82 2002/05/03 10:34:35 andersen Exp $"
361
362 /* The relocatable object is manipulated using elfin types.  */
363
364 #include <stdio.h>
365 #include <elf.h>
366 #include <endian.h>
367
368 #if __BYTE_ORDER == __LITTLE_ENDIAN
369 #define ELFDATAM        ELFDATA2LSB
370 #elif __BYTE_ORDER == __BIG_ENDIAN
371 #define ELFDATAM        ELFDATA2MSB
372 #endif
373
374
375 /* Machine-specific elf macros for i386 et al.  */
376
377 /* the SH changes have only been tested on the SH4 in =little endian= mode */
378 /* I'm not sure about big endian, so let's warn: */
379
380 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
381 #error insmod.c may require changes for use on big endian SH4/SH3
382 #endif
383
384 /* it may or may not work on the SH1/SH2... So let's error on those
385    also */
386 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
387 #error insmod.c may require changes for non-SH3/SH4 use
388 #endif
389
390 #define ELFCLASSM       ELFCLASS32
391
392
393 #if defined(__sh__)
394
395 #define MATCH_MACHINE(x) (x == EM_SH)
396 #define SHT_RELM        SHT_RELA
397 #define Elf32_RelM      Elf32_Rela
398
399 #elif defined(__arm__)
400
401 #define MATCH_MACHINE(x) (x == EM_ARM)
402 #define SHT_RELM        SHT_REL
403 #define Elf32_RelM      Elf32_Rel
404
405 #elif defined(__powerpc__)
406
407 #define MATCH_MACHINE(x) (x == EM_PPC)
408 #define SHT_RELM        SHT_RELA
409 #define Elf32_RelM      Elf32_Rela
410
411 #elif defined(__mips__)
412
413 /* Account for ELF spec changes.  */
414 #ifndef EM_MIPS_RS3_LE
415 #ifdef EM_MIPS_RS4_BE
416 #define EM_MIPS_RS3_LE  EM_MIPS_RS4_BE
417 #else
418 #define EM_MIPS_RS3_LE  10
419 #endif
420 #endif /* !EM_MIPS_RS3_LE */
421
422 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
423 #define SHT_RELM        SHT_REL
424 #define Elf32_RelM      Elf32_Rel
425
426 #elif defined(__i386__)
427
428 /* presumably we can use these for anything but the SH and ARM*/
429 /* this is the previous behavior, but it does result in
430    insmod.c being broken on anything except i386 */
431 #ifndef EM_486
432 #define MATCH_MACHINE(x)  (x == EM_386)
433 #else
434 #define MATCH_MACHINE(x)  (x == EM_386 || x == EM_486)
435 #endif
436
437 #define SHT_RELM        SHT_REL
438 #define Elf32_RelM      Elf32_Rel
439
440 #elif defined(__mc68000__) 
441
442 #define MATCH_MACHINE(x)        (x == EM_68K)
443 #define SHT_RELM                        SHT_RELA
444 #define Elf32_RelM                      Elf32_Rela
445
446 #elif defined (__v850e__)
447
448 #ifndef EM_CYGNUS_V850  /* grumble */
449 #define EM_CYGNUS_V850          0x9080
450 #endif
451
452 #define MATCH_MACHINE(x)        ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
453 #define SHT_RELM                SHT_RELA
454 #define Elf32_RelM              Elf32_Rela
455
456 #define SYMBOL_PREFIX   "_"
457
458 #else
459 #error Sorry, but insmod.c does not yet support this architecture...
460 #endif
461
462 #ifndef ElfW
463 # if ELFCLASSM == ELFCLASS32
464 #  define ElfW(x)  Elf32_ ## x
465 #  define ELFW(x)  ELF32_ ## x
466 # else
467 #  define ElfW(x)  Elf64_ ## x
468 #  define ELFW(x)  ELF64_ ## x
469 # endif
470 #endif
471
472 /* For some reason this is missing from libc5.  */
473 #ifndef ELF32_ST_INFO
474 # define ELF32_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
475 #endif
476
477 #ifndef ELF64_ST_INFO
478 # define ELF64_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
479 #endif
480
481 struct obj_string_patch;
482 struct obj_symbol_patch;
483
484 struct obj_section
485 {
486   ElfW(Shdr) header;
487   const char *name;
488   char *contents;
489   struct obj_section *load_next;
490   int idx;
491 };
492
493 struct obj_symbol
494 {
495   struct obj_symbol *next;      /* hash table link */
496   const char *name;
497   unsigned long value;
498   unsigned long size;
499   int secidx;                   /* the defining section index/module */
500   int info;
501   int ksymidx;                  /* for export to the kernel symtab */
502   int referenced;               /* actually used in the link */
503 };
504
505 /* Hardcode the hash table size.  We shouldn't be needing so many
506    symbols that we begin to degrade performance, and we get a big win
507    by giving the compiler a constant divisor.  */
508
509 #define HASH_BUCKETS  521
510
511 struct obj_file
512 {
513   ElfW(Ehdr) header;
514   ElfW(Addr) baseaddr;
515   struct obj_section **sections;
516   struct obj_section *load_order;
517   struct obj_section **load_order_search_start;
518   struct obj_string_patch *string_patches;
519   struct obj_symbol_patch *symbol_patches;
520   int (*symbol_cmp)(const char *, const char *);
521   unsigned long (*symbol_hash)(const char *);
522   unsigned long local_symtab_size;
523   struct obj_symbol **local_symtab;
524   struct obj_symbol *symtab[HASH_BUCKETS];
525 };
526
527 enum obj_reloc
528 {
529   obj_reloc_ok,
530   obj_reloc_overflow,
531   obj_reloc_dangerous,
532   obj_reloc_unhandled
533 };
534
535 struct obj_string_patch
536 {
537   struct obj_string_patch *next;
538   int reloc_secidx;
539   ElfW(Addr) reloc_offset;
540   ElfW(Addr) string_offset;
541 };
542
543 struct obj_symbol_patch
544 {
545   struct obj_symbol_patch *next;
546   int reloc_secidx;
547   ElfW(Addr) reloc_offset;
548   struct obj_symbol *sym;
549 };
550
551
552 /* Generic object manipulation routines.  */
553
554 static unsigned long obj_elf_hash(const char *);
555
556 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
557
558 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
559                                          const char *name);
560
561 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
562                                   struct obj_symbol *sym);
563
564 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
565 static void obj_set_symbol_compare(struct obj_file *f,
566                             int (*cmp)(const char *, const char *),
567                             unsigned long (*hash)(const char *));
568 #endif
569
570 static struct obj_section *obj_find_section (struct obj_file *f,
571                                            const char *name);
572
573 static void obj_insert_section_load_order (struct obj_file *f,
574                                     struct obj_section *sec);
575
576 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
577                                                 const char *name,
578                                                 unsigned long align,
579                                                 unsigned long size);
580
581 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
582                                                       const char *name,
583                                                       unsigned long align,
584                                                       unsigned long size);
585
586 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
587
588 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
589                      const char *string);
590
591 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
592 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
593                      struct obj_symbol *sym);
594 #endif
595
596 static int obj_check_undefineds(struct obj_file *f);
597
598 static void obj_allocate_commons(struct obj_file *f);
599
600 static unsigned long obj_load_size (struct obj_file *f);
601
602 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
603
604 static struct obj_file *obj_load(FILE *f, int loadprogbits);
605
606 static int obj_create_image (struct obj_file *f, char *image);
607
608 /* Architecture specific manipulation routines.  */
609
610 static struct obj_file *arch_new_file (void);
611
612 static struct obj_section *arch_new_section (void);
613
614 static struct obj_symbol *arch_new_symbol (void);
615
616 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
617                                       struct obj_section *targsec,
618                                       struct obj_section *symsec,
619                                       struct obj_symbol *sym,
620                                       ElfW(RelM) *rel, ElfW(Addr) value);
621
622 static int arch_create_got (struct obj_file *f);
623
624 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
625 static int arch_init_module (struct obj_file *f, struct new_module *);
626 #endif
627
628 #endif /* obj.h */
629 //----------------------------------------------------------------------------
630 //--------end of modutils obj.h
631 //----------------------------------------------------------------------------
632
633
634
635 /* SPFX is always a string, so it can be concatenated to string constants.  */
636 #ifdef SYMBOL_PREFIX
637 #define SPFX    SYMBOL_PREFIX
638 #else
639 #define SPFX    ""
640 #endif
641
642
643 #define _PATH_MODULES   "/lib/modules"
644 static const int STRVERSIONLEN = 32;
645
646 /*======================================================================*/
647
648 static int flag_force_load = 0;
649 static int flag_autoclean = 0;
650 static int flag_verbose = 0;
651 static int flag_export = 1;
652
653
654 /*======================================================================*/
655
656 /* previously, these were named i386_* but since we could be
657    compiling for the sh, I've renamed them to the more general
658    arch_* These structures are the same between the x86 and SH, 
659    and we can't support anything else right now anyway. In the
660    future maybe they should be #if defined'd */
661
662 /* Done ;-) */
663
664
665
666 #if defined(CONFIG_USE_PLT_ENTRIES)
667 struct arch_plt_entry
668 {
669   int offset;
670   int allocated:1;
671   int inited:1;                /* has been set up */
672 };
673 #endif
674
675 #if defined(CONFIG_USE_GOT_ENTRIES)
676 struct arch_got_entry {
677         int offset;
678         unsigned offset_done:1;
679         unsigned reloc_done:1;
680 };
681 #endif
682
683 #if defined(__mips__)
684 struct mips_hi16
685 {
686   struct mips_hi16 *next;
687   Elf32_Addr *addr;
688   Elf32_Addr value;
689 };
690 #endif
691
692 struct arch_file {
693         struct obj_file root;
694 #if defined(CONFIG_USE_PLT_ENTRIES)
695         struct obj_section *plt;
696 #endif
697 #if defined(CONFIG_USE_GOT_ENTRIES)
698         struct obj_section *got;
699 #endif
700 #if defined(__mips__)
701         struct mips_hi16 *mips_hi16_list;
702 #endif
703 };
704
705 struct arch_symbol {
706         struct obj_symbol root;
707 #if defined(CONFIG_USE_PLT_ENTRIES)
708         struct arch_plt_entry pltent;
709 #endif
710 #if defined(CONFIG_USE_GOT_ENTRIES)
711         struct arch_got_entry gotent;
712 #endif
713 };
714
715
716 struct external_module {
717         const char *name;
718         ElfW(Addr) addr;
719         int used;
720         size_t nsyms;
721         struct new_module_symbol *syms;
722 };
723
724 static struct new_module_symbol *ksyms;
725 static size_t nksyms;
726
727 static struct external_module *ext_modules;
728 static int n_ext_modules;
729 static int n_ext_modules_used;
730 extern int delete_module(const char *);
731
732 static char m_filename[FILENAME_MAX + 1];
733 static char m_fullName[FILENAME_MAX + 1];
734
735
736
737 /*======================================================================*/
738
739
740 static int check_module_name_match(const char *filename, struct stat *statbuf,
741                                                    void *userdata)
742 {
743         char *fullname = (char *) userdata;
744
745         if (fullname[0] == '\0')
746                 return (FALSE);
747         else {
748                 char *tmp, *tmp1 = xstrdup(filename);
749                 tmp = get_last_path_component(tmp1);
750                 if (strcmp(tmp, fullname) == 0) {
751                         free(tmp1);
752                         /* Stop searching if we find a match */
753                         safe_strncpy(m_filename, filename, sizeof(m_filename));
754                         return (TRUE);
755                 }
756                 free(tmp1);
757         }
758         return (FALSE);
759 }
760
761
762 /*======================================================================*/
763
764 static struct obj_file *arch_new_file(void)
765 {
766         struct arch_file *f;
767         f = xmalloc(sizeof(*f));
768
769 #if defined(CONFIG_USE_PLT_ENTRIES)
770         f->plt = NULL;
771 #endif
772 #if defined(CONFIG_USE_GOT_ENTRIES)
773         f->got = NULL;
774 #endif
775 #if defined(__mips__)
776         f->mips_hi16_list = NULL;
777 #endif
778
779         return &f->root;
780 }
781
782 static struct obj_section *arch_new_section(void)
783 {
784         return xmalloc(sizeof(struct obj_section));
785 }
786
787 static struct obj_symbol *arch_new_symbol(void)
788 {
789         struct arch_symbol *sym;
790         sym = xmalloc(sizeof(*sym));
791
792 #if defined(CONFIG_USE_PLT_ENTRIES)
793         memset(&sym->pltent, 0, sizeof(sym->pltent));
794 #endif
795 #if defined(CONFIG_USE_GOT_ENTRIES)
796         memset(&sym->gotent, 0, sizeof(sym->gotent));
797 #endif
798
799         return &sym->root;
800 }
801
802 static enum obj_reloc
803 arch_apply_relocation(struct obj_file *f,
804                                           struct obj_section *targsec,
805                                           struct obj_section *symsec,
806                                           struct obj_symbol *sym,
807                                       ElfW(RelM) *rel, ElfW(Addr) v)
808 {
809         struct arch_file *ifile = (struct arch_file *) f;
810 #if !(defined(__mips__))
811         struct arch_symbol *isym = (struct arch_symbol *) sym;
812 #endif
813
814         ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
815         ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
816 #if defined(CONFIG_USE_GOT_ENTRIES)
817         ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
818 #endif
819 #if defined(CONFIG_USE_PLT_ENTRIES)
820         ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
821         struct arch_plt_entry *pe;
822         unsigned long *ip;
823 #endif
824         enum obj_reloc ret = obj_reloc_ok;
825
826         switch (ELF32_R_TYPE(rel->r_info)) {
827
828 /* even though these constants seem to be the same for
829    the i386 and the sh, we "#if define" them for clarity
830    and in case that ever changes */
831 #if defined(__sh__)
832         case R_SH_NONE:
833 #elif defined(__arm__)
834         case R_ARM_NONE:
835 #elif defined(__i386__)
836         case R_386_NONE:
837 #elif defined(__mc68000__) 
838         case R_68K_NONE:
839 #elif defined(__powerpc__)
840         case R_PPC_NONE:
841 #elif defined(__mips__)
842         case R_MIPS_NONE:
843 #elif defined (__v850e__)
844         case R_V850_NONE:
845 #endif
846                 break;
847
848 #if defined (__v850e__)
849         case R_V850_32:
850                 /* We write two shorts instead of a long because even
851                    32-bit insns only need half-word alignment, but
852                    32-bit data needs to be long-word aligned.  */
853                 v += ((unsigned short *)loc)[0];
854                 v += ((unsigned short *)loc)[1] << 16;
855                 ((unsigned short *)loc)[0] = v & 0xffff;
856                 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
857                 break;
858 #else /* !__v850e__ */
859 #if defined(__sh__)
860         case R_SH_DIR32:
861 #elif defined(__arm__)
862         case R_ARM_ABS32:
863 #elif defined(__i386__)
864         case R_386_32:  
865 #elif defined(__mc68000__) 
866         case R_68K_32:
867 #elif defined(__powerpc__)
868         case R_PPC_ADDR32:
869 #elif defined(__mips__)
870         case R_MIPS_32:
871 #endif
872                 *loc += v;
873                 break;
874 #endif /* __v850e__ */
875
876 #if defined(__mc68000__)
877     case R_68K_8:
878                 if (v > 0xff)
879                 ret = obj_reloc_overflow;
880                 *(char *)loc = v;
881                 break;
882     case R_68K_16:
883                 if (v > 0xffff)
884                 ret = obj_reloc_overflow;
885                 *(short *)loc = v;
886                 break;
887 #endif /* __mc68000__   */
888
889 #if defined(__powerpc__)
890         case R_PPC_ADDR16_HA:
891                 *(unsigned short *)loc = (v + 0x8000) >> 16;
892                 break;
893
894         case R_PPC_ADDR16_HI:
895                 *(unsigned short *)loc = v >> 16;
896                 break;
897
898         case R_PPC_ADDR16_LO:
899                 *(unsigned short *)loc = v;
900                 break;
901 #endif
902
903 #if defined(__mips__)
904         case R_MIPS_26:
905                 if (v % 4)
906                         ret = obj_reloc_dangerous;
907                 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
908                         ret = obj_reloc_overflow;
909                 *loc =
910                     (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
911                                             0x03ffffff);
912                 break;
913
914         case R_MIPS_HI16:
915                 {
916                         struct mips_hi16 *n;
917
918                         /* We cannot relocate this one now because we don't know the value
919                            of the carry we need to add.  Save the information, and let LO16
920                            do the actual relocation.  */
921                         n = (struct mips_hi16 *) xmalloc(sizeof *n);
922                         n->addr = loc;
923                         n->value = v;
924                         n->next = ifile->mips_hi16_list;
925                         ifile->mips_hi16_list = n;
926                         break;
927                 }
928
929         case R_MIPS_LO16:
930                 {
931                         unsigned long insnlo = *loc;
932                         Elf32_Addr val, vallo;
933
934                         /* Sign extend the addend we extract from the lo insn.  */
935                         vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
936
937                         if (ifile->mips_hi16_list != NULL) {
938                                 struct mips_hi16 *l;
939
940                                 l = ifile->mips_hi16_list;
941                                 while (l != NULL) {
942                                         struct mips_hi16 *next;
943                                         unsigned long insn;
944
945                                         /* The value for the HI16 had best be the same. */
946                                         assert(v == l->value);
947
948                                         /* Do the HI16 relocation.  Note that we actually don't
949                                            need to know anything about the LO16 itself, except where
950                                            to find the low 16 bits of the addend needed by the LO16.  */
951                                         insn = *l->addr;
952                                         val =
953                                             ((insn & 0xffff) << 16) +
954                                             vallo;
955                                         val += v;
956
957                                         /* Account for the sign extension that will happen in the
958                                            low bits.  */
959                                         val =
960                                             ((val >> 16) +
961                                              ((val & 0x8000) !=
962                                               0)) & 0xffff;
963
964                                         insn = (insn & ~0xffff) | val;
965                                         *l->addr = insn;
966
967                                         next = l->next;
968                                         free(l);
969                                         l = next;
970                                 }
971
972                                 ifile->mips_hi16_list = NULL;
973                         }
974
975                         /* Ok, we're done with the HI16 relocs.  Now deal with the LO16.  */
976                         val = v + vallo;
977                         insnlo = (insnlo & ~0xffff) | (val & 0xffff);
978                         *loc = insnlo;
979                         break;
980                 }
981 #endif
982
983 #if defined(__arm__)
984 #elif defined(__sh__)
985         case R_SH_REL32:
986                 *loc += v - dot;
987                 break;
988 #elif defined(__i386__)
989         case R_386_PLT32:
990         case R_386_PC32:
991                 *loc += v - dot;
992                 break;
993 #elif defined(__mc68000__)
994     case R_68K_PC8:
995                 v -= dot;
996                 if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
997                 ret = obj_reloc_overflow;
998                 *(char *)loc = v;
999     break;
1000                 case R_68K_PC16:
1001                 v -= dot;
1002                 if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1003                 ret = obj_reloc_overflow;
1004                 *(short *)loc = v;
1005                 break;
1006     case R_68K_PC32:
1007                 *(int *)loc = v - dot;
1008                 break;
1009 #elif defined(__powerpc__)
1010         case R_PPC_REL32:
1011                 *loc = v - dot;
1012                 break;
1013 #endif
1014
1015 #if defined(__sh__)
1016         case R_SH_PLT32:
1017                 *loc = v - dot;
1018                 break;
1019 #elif defined(__i386__)
1020 #endif
1021
1022 #if defined(CONFIG_USE_PLT_ENTRIES)
1023
1024 #if defined(__arm__)
1025     case R_ARM_PC24:
1026     case R_ARM_PLT32:
1027 #endif
1028 #if defined(__powerpc__)
1029         case R_PPC_REL24:
1030 #endif
1031 #if defined (__v850e__)
1032         case R_V850_22_PCREL:
1033 #endif
1034       /* find the plt entry and initialize it if necessary */
1035       assert(isym != NULL);
1036
1037       pe = (struct arch_plt_entry*) &isym->pltent;
1038
1039       if (! pe->inited) {
1040                 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1041
1042                 /* generate some machine code */
1043
1044 #if defined(__arm__)
1045                 ip[0] = 0xe51ff004;                     /* ldr pc,[pc,#-4] */
1046                 ip[1] = v;                              /* sym@ */
1047 #endif
1048 #if defined(__powerpc__)
1049           ip[0] = 0x3d600000 + ((v + 0x8000) >> 16);  /* lis r11,sym@ha */
1050           ip[1] = 0x396b0000 + (v & 0xffff);          /* addi r11,r11,sym@l */
1051           ip[2] = 0x7d6903a6;                         /* mtctr r11 */
1052           ip[3] = 0x4e800420;                         /* bctr */
1053 #endif
1054 #if defined (__v850e__)
1055                 /* We have to trash a register, so we assume that any control
1056                    transfer more than 21-bits away must be a function call
1057                    (so we can use a call-clobbered register).  */
1058                 ip[0] = 0x0621 + ((v & 0xffff) << 16);   /* mov sym, r1 ... */
1059                 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1060 #endif
1061                 pe->inited = 1;
1062           }
1063
1064       /* relative distance to target */
1065       v -= dot;
1066       /* if the target is too far away.... */
1067 #if defined (__arm__) || defined (__powerpc__)
1068       if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1069 #elif defined (__v850e__)
1070       if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1071 #endif
1072               /* go via the plt */
1073               v = plt + pe->offset - dot;
1074
1075 #if defined (__v850e__)
1076       if (v & 1)
1077 #else
1078       if (v & 3)
1079 #endif
1080               ret = obj_reloc_dangerous;
1081
1082       /* merge the offset into the instruction. */
1083 #if defined(__arm__)
1084       /* Convert to words. */
1085       v >>= 2;
1086
1087       *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1088 #endif
1089 #if defined(__powerpc__)
1090       *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1091 #endif
1092 #if defined (__v850e__)
1093       /* We write two shorts instead of a long because even 32-bit insns
1094          only need half-word alignment, but the 32-bit data write needs
1095          to be long-word aligned.  */
1096       ((unsigned short *)loc)[0] =
1097               (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1098               | ((v >> 16) & 0x3f);             /* offs high part */
1099       ((unsigned short *)loc)[1] =
1100               (v & 0xffff);                    /* offs low part */
1101 #endif
1102
1103       break;
1104 #endif /* CONFIG_USE_PLT_ENTRIES */
1105
1106 #if defined(__arm__)
1107 #elif defined(__sh__)
1108         case R_SH_GLOB_DAT:
1109         case R_SH_JMP_SLOT:
1110                 *loc = v;
1111                 break;
1112 #elif defined(__i386__)
1113         case R_386_GLOB_DAT:
1114         case R_386_JMP_SLOT:
1115                 *loc = v;
1116                 break;
1117 #elif defined(__mc68000__)
1118         case R_68K_GLOB_DAT:
1119         case R_68K_JMP_SLOT:
1120                 *loc = v;
1121                 break;
1122 #endif
1123
1124 #if defined(__arm__)
1125 #elif defined(__sh__)
1126         case R_SH_RELATIVE:
1127                 *loc += f->baseaddr + rel->r_addend;
1128                 break;
1129 #elif defined(__i386__)
1130         case R_386_RELATIVE:
1131                 *loc += f->baseaddr;
1132                 break;
1133 #elif defined(__mc68000__)
1134     case R_68K_RELATIVE:
1135         *(int *)loc += f->baseaddr;
1136         break;
1137 #endif
1138
1139 #if defined(CONFIG_USE_GOT_ENTRIES)
1140
1141 #if !defined(__68k__)
1142 #if defined(__sh__)
1143         case R_SH_GOTPC:
1144 #elif defined(__arm__)
1145     case R_ARM_GOTPC:
1146 #elif defined(__i386__)
1147         case R_386_GOTPC:
1148 #endif
1149                 assert(got != 0);
1150 #if defined(__sh__)
1151                 *loc += got - dot + rel->r_addend;;
1152 #elif defined(__i386__) || defined(__arm__) || defined(__m68k_)
1153                 *loc += got - dot;
1154 #endif
1155                 break;
1156 #endif // __68k__
1157
1158 #if defined(__sh__)
1159         case R_SH_GOT32:
1160 #elif defined(__arm__)
1161         case R_ARM_GOT32:
1162 #elif defined(__i386__)
1163         case R_386_GOT32:
1164 #elif defined(__mc68000__)
1165         case R_68K_GOT32:
1166 #endif
1167                 assert(isym != NULL);
1168         /* needs an entry in the .got: set it, once */
1169                 if (!isym->gotent.reloc_done) {
1170                         isym->gotent.reloc_done = 1;
1171                         *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1172                 }
1173         /* make the reloc with_respect_to_.got */
1174 #if defined(__sh__)
1175                 *loc += isym->gotent.offset + rel->r_addend;
1176 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1177                 *loc += isym->gotent.offset;
1178 #endif
1179                 break;
1180
1181     /* address relative to the got */
1182 #if !defined(__mc68000__)
1183 #if defined(__sh__)
1184         case R_SH_GOTOFF:
1185 #elif defined(__arm__)
1186         case R_ARM_GOTOFF:
1187 #elif defined(__i386__)
1188         case R_386_GOTOFF:
1189 #elif defined(__mc68000__)
1190         case R_68K_GOTOFF:
1191 #endif
1192                 assert(got != 0);
1193                 *loc += v - got;
1194                 break;
1195 #endif // __mc68000__
1196
1197 #endif /* CONFIG_USE_GOT_ENTRIES */
1198
1199         default:
1200         printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1201                 ret = obj_reloc_unhandled;
1202                 break;
1203         }
1204
1205         return ret;
1206 }
1207
1208 static int arch_create_got(struct obj_file *f)
1209 {
1210 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1211         struct arch_file *ifile = (struct arch_file *) f;
1212         int i;
1213 #if defined(CONFIG_USE_GOT_ENTRIES)
1214         int got_offset = 0, gotneeded = 0;
1215 #endif
1216 #if defined(CONFIG_USE_PLT_ENTRIES)
1217         int plt_offset = 0, pltneeded = 0;
1218 #endif
1219     struct obj_section *relsec, *symsec, *strsec;
1220         ElfW(RelM) *rel, *relend;
1221         ElfW(Sym) *symtab, *extsym;
1222         const char *strtab, *name;
1223         struct arch_symbol *intsym;
1224
1225         for (i = 0; i < f->header.e_shnum; ++i) {
1226                 relsec = f->sections[i];
1227                 if (relsec->header.sh_type != SHT_RELM)
1228                         continue;
1229
1230                 symsec = f->sections[relsec->header.sh_link];
1231                 strsec = f->sections[symsec->header.sh_link];
1232
1233                 rel = (ElfW(RelM) *) relsec->contents;
1234                 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1235                 symtab = (ElfW(Sym) *) symsec->contents;
1236                 strtab = (const char *) strsec->contents;
1237
1238                 for (; rel < relend; ++rel) {
1239                         extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1240
1241                         switch (ELF32_R_TYPE(rel->r_info)) {
1242 #if defined(__arm__)
1243                         case R_ARM_GOT32:
1244                                 break;
1245 #elif defined(__sh__)
1246                         case R_SH_GOT32:
1247                                 break;
1248 #elif defined(__i386__)
1249                         case R_386_GOT32:
1250                                 break;
1251 #elif defined(__mc68000__)
1252                         case R_68K_GOT32:
1253                                 break;
1254 #endif
1255
1256 #if defined(__powerpc__)
1257                         case R_PPC_REL24:
1258                                 pltneeded = 1;
1259                                 break;
1260 #endif
1261
1262 #if defined (__v850e__)
1263                         case R_V850_22_PCREL:
1264                                 pltneeded = 1;
1265                                 break;
1266 #endif
1267
1268 #if defined(__arm__)
1269                         case R_ARM_PC24:
1270                         case R_ARM_PLT32:
1271                                 pltneeded = 1;
1272                                 break;
1273
1274                         case R_ARM_GOTPC:
1275                         case R_ARM_GOTOFF:
1276                                 gotneeded = 1;
1277                                 if (got_offset == 0)
1278                                         got_offset = 4;
1279 #elif defined(__sh__)
1280                         case R_SH_GOTPC:
1281                         case R_SH_GOTOFF:
1282                                 gotneeded = 1;
1283 #elif defined(__i386__)
1284                         case R_386_GOTPC:
1285                         case R_386_GOTOFF:
1286                                 gotneeded = 1;
1287 #endif
1288
1289                         default:
1290                                 continue;
1291                         }
1292
1293                         if (extsym->st_name != 0) {
1294                                 name = strtab + extsym->st_name;
1295                         } else {
1296                                 name = f->sections[extsym->st_shndx]->name;
1297                         }
1298                         intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1299 #if defined(CONFIG_USE_GOT_ENTRIES)
1300                         if (!intsym->gotent.offset_done) {
1301                                 intsym->gotent.offset_done = 1;
1302                                 intsym->gotent.offset = got_offset;
1303                                 got_offset += CONFIG_GOT_ENTRY_SIZE;
1304                         }
1305 #endif
1306 #if defined(CONFIG_USE_PLT_ENTRIES)
1307                         if (pltneeded && intsym->pltent.allocated == 0) {
1308                                 intsym->pltent.allocated = 1;
1309                                 intsym->pltent.offset = plt_offset;
1310                                 plt_offset += CONFIG_PLT_ENTRY_SIZE;
1311                                 intsym->pltent.inited = 0;
1312                                 pltneeded = 0;
1313                         }
1314 #endif
1315                 }
1316         }
1317
1318 #if defined(CONFIG_USE_GOT_ENTRIES)
1319         if (got_offset) {
1320                 struct obj_section* myrelsec = obj_find_section(f, ".got");
1321
1322                 if (myrelsec) {
1323                         obj_extend_section(myrelsec, got_offset);
1324                 } else {
1325                         myrelsec = obj_create_alloced_section(f, ".got", 
1326                                                             CONFIG_GOT_ENTRY_SIZE,
1327                                                             got_offset);
1328                         assert(myrelsec);
1329                 }
1330
1331                 ifile->got = myrelsec;
1332         }
1333 #endif
1334
1335 #if defined(CONFIG_USE_PLT_ENTRIES)
1336         if (plt_offset)
1337                 ifile->plt = obj_create_alloced_section(f, ".plt", 
1338                                                         CONFIG_PLT_ENTRY_SIZE, 
1339                                                         plt_offset);
1340 #endif
1341 #endif
1342         return 1;
1343 }
1344
1345 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1346 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1347 {
1348         return 1;
1349 }
1350 #endif
1351
1352 /*======================================================================*/
1353
1354 /* Standard ELF hash function.  */
1355 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1356 {
1357         unsigned long h = 0;
1358         unsigned long g;
1359         unsigned char ch;
1360
1361         while (n > 0) {
1362                 ch = *name++;
1363                 h = (h << 4) + ch;
1364                 if ((g = (h & 0xf0000000)) != 0) {
1365                         h ^= g >> 24;
1366                         h &= ~g;
1367                 }
1368                 n--;
1369         }
1370         return h;
1371 }
1372
1373 static unsigned long obj_elf_hash(const char *name)
1374 {
1375         return obj_elf_hash_n(name, strlen(name));
1376 }
1377
1378 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1379 /* String comparison for non-co-versioned kernel and module.  */
1380
1381 static int ncv_strcmp(const char *a, const char *b)
1382 {
1383         size_t alen = strlen(a), blen = strlen(b);
1384
1385         if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1386                 return strncmp(a, b, alen);
1387         else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1388                 return strncmp(a, b, blen);
1389         else
1390                 return strcmp(a, b);
1391 }
1392
1393 /* String hashing for non-co-versioned kernel and module.  Here
1394    we are simply forced to drop the crc from the hash.  */
1395
1396 static unsigned long ncv_symbol_hash(const char *str)
1397 {
1398         size_t len = strlen(str);
1399         if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1400                 len -= 10;
1401         return obj_elf_hash_n(str, len);
1402 }
1403
1404 static void
1405 obj_set_symbol_compare(struct obj_file *f,
1406                                            int (*cmp) (const char *, const char *),
1407                                            unsigned long (*hash) (const char *))
1408 {
1409         if (cmp)
1410                 f->symbol_cmp = cmp;
1411         if (hash) {
1412                 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1413                 int i;
1414
1415                 f->symbol_hash = hash;
1416
1417                 memcpy(tmptab, f->symtab, sizeof(tmptab));
1418                 memset(f->symtab, 0, sizeof(f->symtab));
1419
1420                 for (i = 0; i < HASH_BUCKETS; ++i)
1421                         for (sym = tmptab[i]; sym; sym = next) {
1422                                 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1423                                 next = sym->next;
1424                                 sym->next = f->symtab[h];
1425                                 f->symtab[h] = sym;
1426                         }
1427         }
1428 }
1429
1430 #endif                                                  /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1431
1432 static struct obj_symbol *
1433 obj_add_symbol(struct obj_file *f, const char *name,
1434                                                                   unsigned long symidx, int info,
1435                                                                   int secidx, ElfW(Addr) value,
1436                                                                   unsigned long size)
1437 {
1438         struct obj_symbol *sym;
1439         unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1440         int n_type = ELFW(ST_TYPE) (info);
1441         int n_binding = ELFW(ST_BIND) (info);
1442
1443         for (sym = f->symtab[hash]; sym; sym = sym->next)
1444                 if (f->symbol_cmp(sym->name, name) == 0) {
1445                         int o_secidx = sym->secidx;
1446                         int o_info = sym->info;
1447                         int o_type = ELFW(ST_TYPE) (o_info);
1448                         int o_binding = ELFW(ST_BIND) (o_info);
1449
1450                         /* A redefinition!  Is it legal?  */
1451
1452                         if (secidx == SHN_UNDEF)
1453                                 return sym;
1454                         else if (o_secidx == SHN_UNDEF)
1455                                 goto found;
1456                         else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1457                                 /* Cope with local and global symbols of the same name
1458                                    in the same object file, as might have been created
1459                                    by ld -r.  The only reason locals are now seen at this
1460                                    level at all is so that we can do semi-sensible things
1461                                    with parameters.  */
1462
1463                                 struct obj_symbol *nsym, **p;
1464
1465                                 nsym = arch_new_symbol();
1466                                 nsym->next = sym->next;
1467                                 nsym->ksymidx = -1;
1468
1469                                 /* Excise the old (local) symbol from the hash chain.  */
1470                                 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1471                                         continue;
1472                                 *p = sym = nsym;
1473                                 goto found;
1474                         } else if (n_binding == STB_LOCAL) {
1475                                 /* Another symbol of the same name has already been defined.
1476                                    Just add this to the local table.  */
1477                                 sym = arch_new_symbol();
1478                                 sym->next = NULL;
1479                                 sym->ksymidx = -1;
1480                                 f->local_symtab[symidx] = sym;
1481                                 goto found;
1482                         } else if (n_binding == STB_WEAK)
1483                                 return sym;
1484                         else if (o_binding == STB_WEAK)
1485                                 goto found;
1486                         /* Don't unify COMMON symbols with object types the programmer
1487                            doesn't expect.  */
1488                         else if (secidx == SHN_COMMON
1489                                          && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1490                                 return sym;
1491                         else if (o_secidx == SHN_COMMON
1492                                          && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1493                                 goto found;
1494                         else {
1495                                 /* Don't report an error if the symbol is coming from
1496                                    the kernel or some external module.  */
1497                                 if (secidx <= SHN_HIRESERVE)
1498                                         error_msg("%s multiply defined", name);
1499                                 return sym;
1500                         }
1501                 }
1502
1503         /* Completely new symbol.  */
1504         sym = arch_new_symbol();
1505         sym->next = f->symtab[hash];
1506         f->symtab[hash] = sym;
1507         sym->ksymidx = -1;
1508
1509         if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1510                 if (symidx >= f->local_symtab_size)
1511                         error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1512                                         name, (long) symidx, (long) f->local_symtab_size);
1513                 else
1514                         f->local_symtab[symidx] = sym;
1515         }
1516
1517   found:
1518         sym->name = name;
1519         sym->value = value;
1520         sym->size = size;
1521         sym->secidx = secidx;
1522         sym->info = info;
1523
1524         return sym;
1525 }
1526
1527 static struct obj_symbol *
1528 obj_find_symbol(struct obj_file *f, const char *name)
1529 {
1530         struct obj_symbol *sym;
1531         unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1532
1533         for (sym = f->symtab[hash]; sym; sym = sym->next)
1534                 if (f->symbol_cmp(sym->name, name) == 0)
1535                         return sym;
1536
1537         return NULL;
1538 }
1539
1540 static ElfW(Addr)
1541         obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1542 {
1543         if (sym) {
1544                 if (sym->secidx >= SHN_LORESERVE)
1545                         return sym->value;
1546
1547                 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1548         } else {
1549                 /* As a special case, a NULL sym has value zero.  */
1550                 return 0;
1551         }
1552 }
1553
1554 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1555 {
1556         int i, n = f->header.e_shnum;
1557
1558         for (i = 0; i < n; ++i)
1559                 if (strcmp(f->sections[i]->name, name) == 0)
1560                         return f->sections[i];
1561
1562         return NULL;
1563 }
1564
1565 static int obj_load_order_prio(struct obj_section *a)
1566 {
1567         unsigned long af, ac;
1568
1569         af = a->header.sh_flags;
1570
1571         ac = 0;
1572         if (a->name[0] != '.' || strlen(a->name) != 10 ||
1573                 strcmp(a->name + 5, ".init"))
1574                 ac |= 32;
1575         if (af & SHF_ALLOC)
1576                 ac |= 16;
1577         if (!(af & SHF_WRITE))
1578                 ac |= 8;
1579         if (af & SHF_EXECINSTR)
1580                 ac |= 4;
1581         if (a->header.sh_type != SHT_NOBITS)
1582                 ac |= 2;
1583
1584         return ac;
1585 }
1586
1587 static void
1588 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1589 {
1590         struct obj_section **p;
1591         int prio = obj_load_order_prio(sec);
1592         for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1593                 if (obj_load_order_prio(*p) < prio)
1594                         break;
1595         sec->load_next = *p;
1596         *p = sec;
1597 }
1598
1599 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1600                                                                                            const char *name,
1601                                                                                            unsigned long align,
1602                                                                                            unsigned long size)
1603 {
1604         int newidx = f->header.e_shnum++;
1605         struct obj_section *sec;
1606
1607         f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1608         f->sections[newidx] = sec = arch_new_section();
1609
1610         memset(sec, 0, sizeof(*sec));
1611         sec->header.sh_type = SHT_PROGBITS;
1612         sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1613         sec->header.sh_size = size;
1614         sec->header.sh_addralign = align;
1615         sec->name = name;
1616         sec->idx = newidx;
1617         if (size)
1618                 sec->contents = xmalloc(size);
1619
1620         obj_insert_section_load_order(f, sec);
1621
1622         return sec;
1623 }
1624
1625 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1626                                                                                                          const char *name,
1627                                                                                                          unsigned long align,
1628                                                                                                          unsigned long size)
1629 {
1630         int newidx = f->header.e_shnum++;
1631         struct obj_section *sec;
1632
1633         f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1634         f->sections[newidx] = sec = arch_new_section();
1635
1636         memset(sec, 0, sizeof(*sec));
1637         sec->header.sh_type = SHT_PROGBITS;
1638         sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1639         sec->header.sh_size = size;
1640         sec->header.sh_addralign = align;
1641         sec->name = name;
1642         sec->idx = newidx;
1643         if (size)
1644                 sec->contents = xmalloc(size);
1645
1646         sec->load_next = f->load_order;
1647         f->load_order = sec;
1648         if (f->load_order_search_start == &f->load_order)
1649                 f->load_order_search_start = &sec->load_next;
1650
1651         return sec;
1652 }
1653
1654 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1655 {
1656         unsigned long oldsize = sec->header.sh_size;
1657         if (more) { 
1658                 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1659         }
1660         return sec->contents + oldsize;
1661 }
1662
1663
1664 /* Conditionally add the symbols from the given symbol set to the
1665    new module.  */
1666
1667 static int
1668 add_symbols_from(
1669                                  struct obj_file *f,
1670                                  int idx, struct new_module_symbol *syms, size_t nsyms)
1671 {
1672         struct new_module_symbol *s;
1673         size_t i;
1674         int used = 0;
1675 #ifdef SYMBOL_PREFIX
1676         char *name_buf = 0;
1677         size_t name_alloced_size = 0;
1678 #endif
1679
1680         for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1681                 /* Only add symbols that are already marked external.
1682                    If we override locals we may cause problems for
1683                    argument initialization.  We will also create a false
1684                    dependency on the module.  */
1685                 struct obj_symbol *sym;
1686                 char *name = (char *)s->name;
1687
1688 #ifdef SYMBOL_PREFIX
1689                 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1690                    kernel exports `C names', but module object files
1691                    reference `linker names').  */
1692                 size_t extra = sizeof SYMBOL_PREFIX;
1693                 size_t name_size = strlen (name) + extra;
1694                 if (name_size > name_alloced_size) {
1695                         name_alloced_size = name_size * 2;
1696                         name_buf = alloca (name_alloced_size);
1697                 }
1698                 strcpy (name_buf, SYMBOL_PREFIX);
1699                 strcpy (name_buf + extra - 1, name);
1700                 name = name_buf;
1701 #endif /* SYMBOL_PREFIX */
1702
1703                 sym = obj_find_symbol(f, name);
1704                 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1705 #ifdef SYMBOL_PREFIX
1706                         /* Put NAME_BUF into more permanent storage.  */
1707                         name = xmalloc (name_size);
1708                         strcpy (name, name_buf);
1709 #endif
1710                         sym = obj_add_symbol(f, name, -1,
1711                                              ELFW(ST_INFO) (STB_GLOBAL,
1712                                                             STT_NOTYPE),
1713                                              idx, s->value, 0);
1714                         /* Did our symbol just get installed?  If so, mark the
1715                            module as "used".  */
1716                         if (sym->secidx == idx)
1717                                 used = 1;
1718                 }
1719         }
1720
1721         return used;
1722 }
1723
1724 static void add_kernel_symbols(struct obj_file *f)
1725 {
1726         struct external_module *m;
1727         int i, nused = 0;
1728
1729         /* Add module symbols first.  */
1730
1731         for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1732                 if (m->nsyms
1733                         && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1734                                                                 m->nsyms)) m->used = 1, ++nused;
1735
1736         n_ext_modules_used = nused;
1737
1738         /* And finally the symbols from the kernel proper.  */
1739
1740         if (nksyms)
1741                 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1742 }
1743
1744 static char *get_modinfo_value(struct obj_file *f, const char *key)
1745 {
1746         struct obj_section *sec;
1747         char *p, *v, *n, *ep;
1748         size_t klen = strlen(key);
1749
1750         sec = obj_find_section(f, ".modinfo");
1751         if (sec == NULL)
1752                 return NULL;
1753         p = sec->contents;
1754         ep = p + sec->header.sh_size;
1755         while (p < ep) {
1756                 v = strchr(p, '=');
1757                 n = strchr(p, '\0');
1758                 if (v) {
1759                         if (p + klen == v && strncmp(p, key, klen) == 0)
1760                                 return v + 1;
1761                 } else {
1762                         if (p + klen == n && strcmp(p, key) == 0)
1763                                 return n;
1764                 }
1765                 p = n + 1;
1766         }
1767
1768         return NULL;
1769 }
1770
1771
1772 /*======================================================================*/
1773 /* Functions relating to module loading in pre 2.1 kernels.  */
1774
1775 static int
1776 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1777 {
1778         while (argc > 0) {
1779                 char *p, *q;
1780                 struct obj_symbol *sym;
1781                 int *loc;
1782
1783                 p = *argv;
1784                 if ((q = strchr(p, '=')) == NULL) {
1785                         argc--;
1786                         continue;
1787                 }
1788                 *q++ = '\0';
1789
1790                 sym = obj_find_symbol(f, p);
1791
1792                 /* Also check that the parameter was not resolved from the kernel.  */
1793                 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1794                         error_msg("symbol for parameter %s not found", p);
1795                         return 0;
1796                 }
1797
1798                 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1799
1800                 /* Do C quoting if we begin with a ".  */
1801                 if (*q == '"') {
1802                         char *r, *str;
1803
1804                         str = alloca(strlen(q));
1805                         for (r = str, q++; *q != '"'; ++q, ++r) {
1806                                 if (*q == '\0') {
1807                                         error_msg("improperly terminated string argument for %s", p);
1808                                         return 0;
1809                                 } else if (*q == '\\')
1810                                         switch (*++q) {
1811                                         case 'a':
1812                                                 *r = '\a';
1813                                                 break;
1814                                         case 'b':
1815                                                 *r = '\b';
1816                                                 break;
1817                                         case 'e':
1818                                                 *r = '\033';
1819                                                 break;
1820                                         case 'f':
1821                                                 *r = '\f';
1822                                                 break;
1823                                         case 'n':
1824                                                 *r = '\n';
1825                                                 break;
1826                                         case 'r':
1827                                                 *r = '\r';
1828                                                 break;
1829                                         case 't':
1830                                                 *r = '\t';
1831                                                 break;
1832
1833                                         case '0':
1834                                         case '1':
1835                                         case '2':
1836                                         case '3':
1837                                         case '4':
1838                                         case '5':
1839                                         case '6':
1840                                         case '7':
1841                                                 {
1842                                                         int c = *q - '0';
1843                                                         if (q[1] >= '0' && q[1] <= '7') {
1844                                                                 c = (c * 8) + *++q - '0';
1845                                                                 if (q[1] >= '0' && q[1] <= '7')
1846                                                                         c = (c * 8) + *++q - '0';
1847                                                         }
1848                                                         *r = c;
1849                                                 }
1850                                                 break;
1851
1852                                         default:
1853                                                 *r = *q;
1854                                                 break;
1855                                 } else
1856                                         *r = *q;
1857                         }
1858                         *r = '\0';
1859                         obj_string_patch(f, sym->secidx, sym->value, str);
1860                 } else if (*q >= '0' && *q <= '9') {
1861                         do
1862                                 *loc++ = strtoul(q, &q, 0);
1863                         while (*q++ == ',');
1864                 } else {
1865                         char *contents = f->sections[sym->secidx]->contents;
1866                         char *myloc = contents + sym->value;
1867                         char *r;                        /* To search for commas */
1868
1869                         /* Break the string with comas */
1870                         while ((r = strchr(q, ',')) != (char *) NULL) {
1871                                 *r++ = '\0';
1872                                 obj_string_patch(f, sym->secidx, myloc - contents, q);
1873                                 myloc += sizeof(char *);
1874                                 q = r;
1875                         }
1876
1877                         /* last part */
1878                         obj_string_patch(f, sym->secidx, myloc - contents, q);
1879                 }
1880
1881                 argc--, argv++;
1882         }
1883
1884         return 1;
1885 }
1886
1887 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1888 static int old_is_module_checksummed(struct obj_file *f)
1889 {
1890         return obj_find_symbol(f, "Using_Versions") != NULL;
1891 }
1892 /* Get the module's kernel version in the canonical integer form.  */
1893
1894 static int
1895 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1896 {
1897         struct obj_symbol *sym;
1898         char *p, *q;
1899         int a, b, c;
1900
1901         sym = obj_find_symbol(f, "kernel_version");
1902         if (sym == NULL)
1903                 return -1;
1904
1905         p = f->sections[sym->secidx]->contents + sym->value;
1906         strncpy(str, p, STRVERSIONLEN);
1907
1908         a = strtoul(p, &p, 10);
1909         if (*p != '.')
1910                 return -1;
1911         b = strtoul(p + 1, &p, 10);
1912         if (*p != '.')
1913                 return -1;
1914         c = strtoul(p + 1, &q, 10);
1915         if (p + 1 == q)
1916                 return -1;
1917
1918         return a << 16 | b << 8 | c;
1919 }
1920
1921 #endif   /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1922
1923 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
1924
1925 /* Fetch all the symbols and divvy them up as appropriate for the modules.  */
1926
1927 static int old_get_kernel_symbols(const char *m_name)
1928 {
1929         struct old_kernel_sym *ks, *k;
1930         struct new_module_symbol *s;
1931         struct external_module *mod;
1932         int nks, nms, nmod, i;
1933
1934         nks = get_kernel_syms(NULL);
1935         if (nks <= 0) {
1936                 if (nks)
1937                         perror_msg("get_kernel_syms: %s", m_name);
1938                 else
1939                         error_msg("No kernel symbols");
1940                 return 0;
1941         }
1942
1943         ks = k = xmalloc(nks * sizeof(*ks));
1944
1945         if (get_kernel_syms(ks) != nks) {
1946                 perror("inconsistency with get_kernel_syms -- is someone else "
1947                            "playing with modules?");
1948                 free(ks);
1949                 return 0;
1950         }
1951
1952         /* Collect the module information.  */
1953
1954         mod = NULL;
1955         nmod = -1;
1956
1957         while (k->name[0] == '#' && k->name[1]) {
1958                 struct old_kernel_sym *k2;
1959
1960                 /* Find out how many symbols this module has.  */
1961                 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1962                         continue;
1963                 nms = k2 - k - 1;
1964
1965                 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1966                 mod[nmod].name = k->name + 1;
1967                 mod[nmod].addr = k->value;
1968                 mod[nmod].used = 0;
1969                 mod[nmod].nsyms = nms;
1970                 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1971
1972                 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1973                         s->name = (unsigned long) k->name;
1974                         s->value = k->value;
1975                 }
1976
1977                 k = k2;
1978         }
1979
1980         ext_modules = mod;
1981         n_ext_modules = nmod + 1;
1982
1983         /* Now collect the symbols for the kernel proper.  */
1984
1985         if (k->name[0] == '#')
1986                 ++k;
1987
1988         nksyms = nms = nks - (k - ks);
1989         ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1990
1991         for (i = 0; i < nms; ++i, ++s, ++k) {
1992                 s->name = (unsigned long) k->name;
1993                 s->value = k->value;
1994         }
1995
1996         return 1;
1997 }
1998
1999 /* Return the kernel symbol checksum version, or zero if not used.  */
2000
2001 static int old_is_kernel_checksummed(void)
2002 {
2003         /* Using_Versions is the first symbol.  */
2004         if (nksyms > 0
2005                 && strcmp((char *) ksyms[0].name,
2006                                   "Using_Versions") == 0) return ksyms[0].value;
2007         else
2008                 return 0;
2009 }
2010
2011
2012 static int old_create_mod_use_count(struct obj_file *f)
2013 {
2014         struct obj_section *sec;
2015
2016         sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2017                                                                                    sizeof(long));
2018
2019         obj_add_symbol(f, "mod_use_count_", -1,
2020                                    ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2021                                    sizeof(long));
2022
2023         return 1;
2024 }
2025
2026 static int
2027 old_init_module(const char *m_name, struct obj_file *f,
2028                                 unsigned long m_size)
2029 {
2030         char *image;
2031         struct old_mod_routines routines;
2032         struct old_symbol_table *symtab;
2033         int ret;
2034
2035         /* Create the symbol table */
2036         {
2037                 int nsyms = 0, strsize = 0, total;
2038
2039                 /* Size things first... */
2040                 if (flag_export) {
2041                         int i;
2042                         for (i = 0; i < HASH_BUCKETS; ++i) {
2043                                 struct obj_symbol *sym;
2044                                 for (sym = f->symtab[i]; sym; sym = sym->next)
2045                                         if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2046                                                 && sym->secidx <= SHN_HIRESERVE) 
2047                                         {
2048                                                 sym->ksymidx = nsyms++;
2049                                                 strsize += strlen(sym->name) + 1;
2050                                         }
2051                         }
2052                 }
2053
2054                 total = (sizeof(struct old_symbol_table)
2055                                  + nsyms * sizeof(struct old_module_symbol)
2056                                  + n_ext_modules_used * sizeof(struct old_module_ref)
2057                                  + strsize);
2058                 symtab = xmalloc(total);
2059                 symtab->size = total;
2060                 symtab->n_symbols = nsyms;
2061                 symtab->n_refs = n_ext_modules_used;
2062
2063                 if (flag_export && nsyms) {
2064                         struct old_module_symbol *ksym;
2065                         char *str;
2066                         int i;
2067
2068                         ksym = symtab->symbol;
2069                         str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2070                                    + n_ext_modules_used * sizeof(struct old_module_ref));
2071
2072                         for (i = 0; i < HASH_BUCKETS; ++i) {
2073                                 struct obj_symbol *sym;
2074                                 for (sym = f->symtab[i]; sym; sym = sym->next)
2075                                         if (sym->ksymidx >= 0) {
2076                                                 ksym->addr = obj_symbol_final_value(f, sym);
2077                                                 ksym->name =
2078                                                         (unsigned long) str - (unsigned long) symtab;
2079
2080                                                 strcpy(str, sym->name);
2081                                                 str += strlen(sym->name) + 1;
2082                                                 ksym++;
2083                                         }
2084                         }
2085                 }
2086
2087                 if (n_ext_modules_used) {
2088                         struct old_module_ref *ref;
2089                         int i;
2090
2091                         ref = (struct old_module_ref *)
2092                                 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2093
2094                         for (i = 0; i < n_ext_modules; ++i)
2095                                 if (ext_modules[i].used)
2096                                         ref++->module = ext_modules[i].addr;
2097                 }
2098         }
2099
2100         /* Fill in routines.  */
2101
2102         routines.init =
2103                 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2104         routines.cleanup =
2105                 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2106
2107         /* Whew!  All of the initialization is complete.  Collect the final
2108            module image and give it to the kernel.  */
2109
2110         image = xmalloc(m_size);
2111         obj_create_image(f, image);
2112
2113         /* image holds the complete relocated module, accounting correctly for
2114            mod_use_count.  However the old module kernel support assume that
2115            it is receiving something which does not contain mod_use_count.  */
2116         ret = old_sys_init_module(m_name, image + sizeof(long),
2117                                                           m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2118                                                                                 : 0), &routines, symtab);
2119         if (ret)
2120                 perror_msg("init_module: %s", m_name);
2121
2122         free(image);
2123         free(symtab);
2124
2125         return ret == 0;
2126 }
2127
2128 #else
2129
2130 #define old_create_mod_use_count(x) TRUE
2131 #define old_init_module(x, y, z) TRUE
2132
2133 #endif                                                  /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2134
2135
2136
2137 /*======================================================================*/
2138 /* Functions relating to module loading after 2.1.18.  */
2139
2140 static int
2141 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2142 {
2143         while (argc > 0) {
2144                 char *p, *q, *key, *sym_name;
2145                 struct obj_symbol *sym;
2146                 char *contents, *loc;
2147                 int min, max, n;
2148
2149                 p = *argv;
2150                 if ((q = strchr(p, '=')) == NULL) {
2151                         argc--;
2152                         continue;
2153                 }
2154
2155                 key = alloca(q - p + 6);
2156                 memcpy(key, "parm_", 5);
2157                 memcpy(key + 5, p, q - p);
2158                 key[q - p + 5] = 0;
2159
2160                 p = get_modinfo_value(f, key);
2161                 key += 5;
2162                 if (p == NULL) {
2163                         error_msg("invalid parameter %s", key);
2164                         return 0;
2165                 }
2166
2167 #ifdef SYMBOL_PREFIX
2168                 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2169                 strcpy (sym_name, SYMBOL_PREFIX);
2170                 strcat (sym_name, key);
2171 #else
2172                 sym_name = key;
2173 #endif
2174                 sym = obj_find_symbol(f, sym_name);
2175
2176                 /* Also check that the parameter was not resolved from the kernel.  */
2177                 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2178                         error_msg("symbol for parameter %s not found", key);
2179                         return 0;
2180                 }
2181
2182                 if (isdigit(*p)) {
2183                         min = strtoul(p, &p, 10);
2184                         if (*p == '-')
2185                                 max = strtoul(p + 1, &p, 10);
2186                         else
2187                                 max = min;
2188                 } else
2189                         min = max = 1;
2190
2191                 contents = f->sections[sym->secidx]->contents;
2192                 loc = contents + sym->value;
2193                 n = (*++q != '\0');
2194
2195                 while (1) {
2196                         if ((*p == 's') || (*p == 'c')) {
2197                                 char *str;
2198
2199                                 /* Do C quoting if we begin with a ", else slurp the lot.  */
2200                                 if (*q == '"') {
2201                                         char *r;
2202
2203                                         str = alloca(strlen(q));
2204                                         for (r = str, q++; *q != '"'; ++q, ++r) {
2205                                                 if (*q == '\0') {
2206                                                         error_msg("improperly terminated string argument for %s",
2207                                                                         key);
2208                                                         return 0;
2209                                                 } else if (*q == '\\')
2210                                                         switch (*++q) {
2211                                                         case 'a':
2212                                                                 *r = '\a';
2213                                                                 break;
2214                                                         case 'b':
2215                                                                 *r = '\b';
2216                                                                 break;
2217                                                         case 'e':
2218                                                                 *r = '\033';
2219                                                                 break;
2220                                                         case 'f':
2221                                                                 *r = '\f';
2222                                                                 break;
2223                                                         case 'n':
2224                                                                 *r = '\n';
2225                                                                 break;
2226                                                         case 'r':
2227                                                                 *r = '\r';
2228                                                                 break;
2229                                                         case 't':
2230                                                                 *r = '\t';
2231                                                                 break;
2232
2233                                                         case '0':
2234                                                         case '1':
2235                                                         case '2':
2236                                                         case '3':
2237                                                         case '4':
2238                                                         case '5':
2239                                                         case '6':
2240                                                         case '7':
2241                                                                 {
2242                                                                         int c = *q - '0';
2243                                                                         if (q[1] >= '0' && q[1] <= '7') {
2244                                                                                 c = (c * 8) + *++q - '0';
2245                                                                                 if (q[1] >= '0' && q[1] <= '7')
2246                                                                                         c = (c * 8) + *++q - '0';
2247                                                                         }
2248                                                                         *r = c;
2249                                                                 }
2250                                                                 break;
2251
2252                                                         default:
2253                                                                 *r = *q;
2254                                                                 break;
2255                                                 } else
2256                                                         *r = *q;
2257                                         }
2258                                         *r = '\0';
2259                                         ++q;
2260                                 } else {
2261                                         char *r;
2262
2263                                         /* In this case, the string is not quoted. We will break
2264                                            it using the coma (like for ints). If the user wants to
2265                                            include comas in a string, he just has to quote it */
2266
2267                                         /* Search the next coma */
2268                                         r = strchr(q, ',');
2269
2270                                         /* Found ? */
2271                                         if (r != (char *) NULL) {
2272                                                 /* Recopy the current field */
2273                                                 str = alloca(r - q + 1);
2274                                                 memcpy(str, q, r - q);
2275
2276                                                 /* I don't know if it is usefull, as the previous case
2277                                                    doesn't null terminate the string ??? */
2278                                                 str[r - q] = '\0';
2279
2280                                                 /* Keep next fields */
2281                                                 q = r;
2282                                         } else {
2283                                                 /* last string */
2284                                                 str = q;
2285                                                 q = "";
2286                                         }
2287                                 }
2288
2289                                 if (*p == 's') {
2290                                         /* Normal string */
2291                                         obj_string_patch(f, sym->secidx, loc - contents, str);
2292                                         loc += tgt_sizeof_char_p;
2293                                 } else {
2294                                         /* Array of chars (in fact, matrix !) */
2295                                         unsigned long charssize;        /* size of each member */
2296
2297                                         /* Get the size of each member */
2298                                         /* Probably we should do that outside the loop ? */
2299                                         if (!isdigit(*(p + 1))) {
2300                                                 error_msg("parameter type 'c' for %s must be followed by"
2301                                                                 " the maximum size", key);
2302                                                 return 0;
2303                                         }
2304                                         charssize = strtoul(p + 1, (char **) NULL, 10);
2305
2306                                         /* Check length */
2307                                         if (strlen(str) >= charssize) {
2308                                                 error_msg("string too long for %s (max %ld)", key,
2309                                                                 charssize - 1);
2310                                                 return 0;
2311                                         }
2312
2313                                         /* Copy to location */
2314                                         strcpy((char *) loc, str);
2315                                         loc += charssize;
2316                                 }
2317                         } else {
2318                                 long v = strtoul(q, &q, 0);
2319                                 switch (*p) {
2320                                 case 'b':
2321                                         *loc++ = v;
2322                                         break;
2323                                 case 'h':
2324                                         *(short *) loc = v;
2325                                         loc += tgt_sizeof_short;
2326                                         break;
2327                                 case 'i':
2328                                         *(int *) loc = v;
2329                                         loc += tgt_sizeof_int;
2330                                         break;
2331                                 case 'l':
2332                                         *(long *) loc = v;
2333                                         loc += tgt_sizeof_long;
2334                                         break;
2335
2336                                 default:
2337                                         error_msg("unknown parameter type '%c' for %s", *p, key);
2338                                         return 0;
2339                                 }
2340                         }
2341
2342                   retry_end_of_value:
2343                         switch (*q) {
2344                         case '\0':
2345                                 goto end_of_arg;
2346
2347                         case ' ':
2348                         case '\t':
2349                         case '\n':
2350                         case '\r':
2351                                 ++q;
2352                                 goto retry_end_of_value;
2353
2354                         case ',':
2355                                 if (++n > max) {
2356                                         error_msg("too many values for %s (max %d)", key, max);
2357                                         return 0;
2358                                 }
2359                                 ++q;
2360                                 break;
2361
2362                         default:
2363                                 error_msg("invalid argument syntax for %s", key);
2364                                 return 0;
2365                         }
2366                 }
2367
2368           end_of_arg:
2369                 if (n < min) {
2370                         error_msg("too few values for %s (min %d)", key, min);
2371                         return 0;
2372                 }
2373
2374                 argc--, argv++;
2375         }
2376
2377         return 1;
2378 }
2379
2380 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2381 static int new_is_module_checksummed(struct obj_file *f)
2382 {
2383         const char *p = get_modinfo_value(f, "using_checksums");
2384         if (p)
2385                 return atoi(p);
2386         else
2387                 return 0;
2388 }
2389
2390 /* Get the module's kernel version in the canonical integer form.  */
2391
2392 static int
2393 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2394 {
2395         char *p, *q;
2396         int a, b, c;
2397
2398         p = get_modinfo_value(f, "kernel_version");
2399         if (p == NULL)
2400                 return -1;
2401         strncpy(str, p, STRVERSIONLEN);
2402
2403         a = strtoul(p, &p, 10);
2404         if (*p != '.')
2405                 return -1;
2406         b = strtoul(p + 1, &p, 10);
2407         if (*p != '.')
2408                 return -1;
2409         c = strtoul(p + 1, &q, 10);
2410         if (p + 1 == q)
2411                 return -1;
2412
2413         return a << 16 | b << 8 | c;
2414 }
2415
2416 #endif   /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2417
2418
2419 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2420
2421 /* Fetch the loaded modules, and all currently exported symbols.  */
2422
2423 static int new_get_kernel_symbols(void)
2424 {
2425         char *module_names, *mn;
2426         struct external_module *modules, *m;
2427         struct new_module_symbol *syms, *s;
2428         size_t ret, bufsize, nmod, nsyms, i, j;
2429
2430         /* Collect the loaded modules.  */
2431
2432         module_names = xmalloc(bufsize = 256);
2433   retry_modules_load:
2434         if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2435                 if (errno == ENOSPC && bufsize < ret) {
2436                         module_names = xrealloc(module_names, bufsize = ret);
2437                         goto retry_modules_load;
2438                 }
2439                 perror_msg("QM_MODULES");
2440                 return 0;
2441         }
2442
2443         n_ext_modules = nmod = ret;
2444
2445         /* Collect the modules' symbols.  */
2446
2447         if (nmod){
2448                 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2449                 memset(modules, 0, nmod * sizeof(*modules));
2450                 for (i = 0, mn = module_names, m = modules;
2451                          i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2452                         struct new_module_info info;
2453         
2454                         if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2455                                 if (errno == ENOENT) {
2456                                         /* The module was removed out from underneath us.  */
2457                                         continue;
2458                                 }
2459                                 perror_msg("query_module: QM_INFO: %s", mn);
2460                                 return 0;
2461                         }
2462         
2463                         syms = xmalloc(bufsize = 1024);
2464                   retry_mod_sym_load:
2465                         if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2466                                 switch (errno) {
2467                                 case ENOSPC:
2468                                         syms = xrealloc(syms, bufsize = ret);
2469                                         goto retry_mod_sym_load;
2470                                 case ENOENT:
2471                                         /* The module was removed out from underneath us.  */
2472                                         continue;
2473                                 default:
2474                                         perror_msg("query_module: QM_SYMBOLS: %s", mn);
2475                                         return 0;
2476                                 }
2477                         }
2478                         nsyms = ret;
2479         
2480                         m->name = mn;
2481                         m->addr = info.addr;
2482                         m->nsyms = nsyms;
2483                         m->syms = syms;
2484         
2485                         for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2486                                 s->name += (unsigned long) syms;
2487                         }
2488                 }
2489         }
2490
2491         /* Collect the kernel's symbols.  */
2492
2493         syms = xmalloc(bufsize = 16 * 1024);
2494   retry_kern_sym_load:
2495         if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2496                 if (errno == ENOSPC && bufsize < ret) {
2497                         syms = xrealloc(syms, bufsize = ret);
2498                         goto retry_kern_sym_load;
2499                 }
2500                 perror_msg("kernel: QM_SYMBOLS");
2501                 return 0;
2502         }
2503         nksyms = nsyms = ret;
2504         ksyms = syms;
2505
2506         for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2507                 s->name += (unsigned long) syms;
2508         }
2509         return 1;
2510 }
2511
2512
2513 /* Return the kernel symbol checksum version, or zero if not used.  */
2514
2515 static int new_is_kernel_checksummed(void)
2516 {
2517         struct new_module_symbol *s;
2518         size_t i;
2519
2520         /* Using_Versions is not the first symbol, but it should be in there.  */
2521
2522         for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2523                 if (strcmp((char *) s->name, "Using_Versions") == 0)
2524                         return s->value;
2525
2526         return 0;
2527 }
2528
2529
2530 static int new_create_this_module(struct obj_file *f, const char *m_name)
2531 {
2532         struct obj_section *sec;
2533
2534         sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2535                                                                                    sizeof(struct new_module));
2536         memset(sec->contents, 0, sizeof(struct new_module));
2537
2538         obj_add_symbol(f, SPFX "__this_module", -1,
2539                        ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2540                        sizeof(struct new_module));
2541
2542         obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2543                                          m_name);
2544
2545         return 1;
2546 }
2547
2548
2549 static int new_create_module_ksymtab(struct obj_file *f)
2550 {
2551         struct obj_section *sec;
2552         int i;
2553
2554         /* We must always add the module references.  */
2555
2556         if (n_ext_modules_used) {
2557                 struct new_module_ref *dep;
2558                 struct obj_symbol *tm;
2559
2560                 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2561                                                                                  (sizeof(struct new_module_ref)
2562                                                                                   * n_ext_modules_used));
2563                 if (!sec)
2564                         return 0;
2565
2566                 tm = obj_find_symbol(f, SPFX "__this_module");
2567                 dep = (struct new_module_ref *) sec->contents;
2568                 for (i = 0; i < n_ext_modules; ++i)
2569                         if (ext_modules[i].used) {
2570                                 dep->dep = ext_modules[i].addr;
2571                                 obj_symbol_patch(f, sec->idx,
2572                                                                  (char *) &dep->ref - sec->contents, tm);
2573                                 dep->next_ref = 0;
2574                                 ++dep;
2575                         }
2576         }
2577
2578         if (flag_export && !obj_find_section(f, "__ksymtab")) {
2579                 size_t nsyms;
2580                 int *loaded;
2581
2582                 sec =
2583                         obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2584                                                                            0);
2585
2586                 /* We don't want to export symbols residing in sections that
2587                    aren't loaded.  There are a number of these created so that
2588                    we make sure certain module options don't appear twice.  */
2589
2590                 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2591                 while (--i >= 0)
2592                         loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2593
2594                 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2595                         struct obj_symbol *sym;
2596                         for (sym = f->symtab[i]; sym; sym = sym->next)
2597                                 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2598                                         && sym->secidx <= SHN_HIRESERVE
2599                                         && (sym->secidx >= SHN_LORESERVE
2600                                                 || loaded[sym->secidx])) {
2601                                         ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2602
2603                                         obj_symbol_patch(f, sec->idx, ofs, sym);
2604                                         obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2605                                                                          sym->name);
2606
2607                                         nsyms++;
2608                                 }
2609                 }
2610
2611                 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2612         }
2613
2614         return 1;
2615 }
2616
2617
2618 static int
2619 new_init_module(const char *m_name, struct obj_file *f,
2620                                 unsigned long m_size)
2621 {
2622         struct new_module *module;
2623         struct obj_section *sec;
2624         void *image;
2625         int ret;
2626         tgt_long m_addr;
2627
2628         sec = obj_find_section(f, ".this");
2629         if (!sec || !sec->contents) { 
2630                 perror_msg_and_die("corrupt module %s?",m_name);
2631         }
2632         module = (struct new_module *) sec->contents;
2633         m_addr = sec->header.sh_addr;
2634
2635         module->size_of_struct = sizeof(*module);
2636         module->size = m_size;
2637         module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2638
2639         sec = obj_find_section(f, "__ksymtab");
2640         if (sec && sec->header.sh_size) {
2641                 module->syms = sec->header.sh_addr;
2642                 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2643         }
2644
2645         if (n_ext_modules_used) {
2646                 sec = obj_find_section(f, ".kmodtab");
2647                 module->deps = sec->header.sh_addr;
2648                 module->ndeps = n_ext_modules_used;
2649         }
2650
2651         module->init =
2652                 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2653         module->cleanup =
2654                 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2655
2656         sec = obj_find_section(f, "__ex_table");
2657         if (sec) {
2658                 module->ex_table_start = sec->header.sh_addr;
2659                 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2660         }
2661
2662         sec = obj_find_section(f, ".text.init");
2663         if (sec) {
2664                 module->runsize = sec->header.sh_addr - m_addr;
2665         }
2666         sec = obj_find_section(f, ".data.init");
2667         if (sec) {
2668                 if (!module->runsize ||
2669                         module->runsize > sec->header.sh_addr - m_addr)
2670                                 module->runsize = sec->header.sh_addr - m_addr;
2671         }
2672         sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2673         if (sec && sec->header.sh_size) {
2674                 module->archdata_start = (void*)sec->header.sh_addr;
2675                 module->archdata_end = module->archdata_start + sec->header.sh_size;
2676         }
2677         sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2678         if (sec && sec->header.sh_size) {
2679                 module->kallsyms_start = (void*)sec->header.sh_addr;
2680                 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2681         }
2682
2683         if (!arch_init_module(f, module))
2684                 return 0;
2685
2686         /* Whew!  All of the initialization is complete.  Collect the final
2687            module image and give it to the kernel.  */
2688
2689         image = xmalloc(m_size);
2690         obj_create_image(f, image);
2691
2692         ret = new_sys_init_module(m_name, (struct new_module *) image);
2693         if (ret)
2694                 perror_msg("init_module: %s", m_name);
2695
2696         free(image);
2697
2698         return ret == 0;
2699 }
2700
2701 #else
2702
2703 #define new_init_module(x, y, z) TRUE
2704 #define new_create_this_module(x, y) 0
2705 #define new_create_module_ksymtab(x)
2706 #define query_module(v, w, x, y, z) -1
2707
2708 #endif                                                  /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
2709
2710
2711 /*======================================================================*/
2712
2713 static int
2714 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2715                                  const char *string)
2716 {
2717         struct obj_string_patch *p;
2718         struct obj_section *strsec;
2719         size_t len = strlen(string) + 1;
2720         char *loc;
2721
2722         p = xmalloc(sizeof(*p));
2723         p->next = f->string_patches;
2724         p->reloc_secidx = secidx;
2725         p->reloc_offset = offset;
2726         f->string_patches = p;
2727
2728         strsec = obj_find_section(f, ".kstrtab");
2729         if (strsec == NULL) {
2730                 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2731                 p->string_offset = 0;
2732                 loc = strsec->contents;
2733         } else {
2734                 p->string_offset = strsec->header.sh_size;
2735                 loc = obj_extend_section(strsec, len);
2736         }
2737         memcpy(loc, string, len);
2738
2739         return 1;
2740 }
2741
2742 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2743 static int
2744 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2745                                  struct obj_symbol *sym)
2746 {
2747         struct obj_symbol_patch *p;
2748
2749         p = xmalloc(sizeof(*p));
2750         p->next = f->symbol_patches;
2751         p->reloc_secidx = secidx;
2752         p->reloc_offset = offset;
2753         p->sym = sym;
2754         f->symbol_patches = p;
2755
2756         return 1;
2757 }
2758 #endif
2759
2760 static int obj_check_undefineds(struct obj_file *f)
2761 {
2762         unsigned long i;
2763         int ret = 1;
2764
2765         for (i = 0; i < HASH_BUCKETS; ++i) {
2766                 struct obj_symbol *sym;
2767                 for (sym = f->symtab[i]; sym; sym = sym->next)
2768                         if (sym->secidx == SHN_UNDEF) {
2769                                 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2770                                         sym->secidx = SHN_ABS;
2771                                         sym->value = 0;
2772                                 } else {
2773                                         error_msg("unresolved symbol %s", sym->name);
2774                                         ret = 0;
2775                                 }
2776                         }
2777         }
2778
2779         return ret;
2780 }
2781
2782 static void obj_allocate_commons(struct obj_file *f)
2783 {
2784         struct common_entry {
2785                 struct common_entry *next;
2786                 struct obj_symbol *sym;
2787         } *common_head = NULL;
2788
2789         unsigned long i;
2790
2791         for (i = 0; i < HASH_BUCKETS; ++i) {
2792                 struct obj_symbol *sym;
2793                 for (sym = f->symtab[i]; sym; sym = sym->next)
2794                         if (sym->secidx == SHN_COMMON) {
2795                                 /* Collect all COMMON symbols and sort them by size so as to
2796                                    minimize space wasted by alignment requirements.  */
2797                                 {
2798                                         struct common_entry **p, *n;
2799                                         for (p = &common_head; *p; p = &(*p)->next)
2800                                                 if (sym->size <= (*p)->sym->size)
2801                                                         break;
2802
2803                                         n = alloca(sizeof(*n));
2804                                         n->next = *p;
2805                                         n->sym = sym;
2806                                         *p = n;
2807                                 }
2808                         }
2809         }
2810
2811         for (i = 1; i < f->local_symtab_size; ++i) {
2812                 struct obj_symbol *sym = f->local_symtab[i];
2813                 if (sym && sym->secidx == SHN_COMMON) {
2814                         struct common_entry **p, *n;
2815                         for (p = &common_head; *p; p = &(*p)->next)
2816                                 if (sym == (*p)->sym)
2817                                         break;
2818                                 else if (sym->size < (*p)->sym->size) {
2819                                         n = alloca(sizeof(*n));
2820                                         n->next = *p;
2821                                         n->sym = sym;
2822                                         *p = n;
2823                                         break;
2824                                 }
2825                 }
2826         }
2827
2828         if (common_head) {
2829                 /* Find the bss section.  */
2830                 for (i = 0; i < f->header.e_shnum; ++i)
2831                         if (f->sections[i]->header.sh_type == SHT_NOBITS)
2832                                 break;
2833
2834                 /* If for some reason there hadn't been one, create one.  */
2835                 if (i == f->header.e_shnum) {
2836                         struct obj_section *sec;
2837
2838                         f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2839                         f->sections[i] = sec = arch_new_section();
2840                         f->header.e_shnum = i + 1;
2841
2842                         memset(sec, 0, sizeof(*sec));
2843                         sec->header.sh_type = SHT_PROGBITS;
2844                         sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2845                         sec->name = ".bss";
2846                         sec->idx = i;
2847                 }
2848
2849                 /* Allocate the COMMONS.  */
2850                 {
2851                         ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2852                         ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2853                         struct common_entry *c;
2854
2855                         for (c = common_head; c; c = c->next) {
2856                                 ElfW(Addr) align = c->sym->value;
2857
2858                                 if (align > max_align)
2859                                         max_align = align;
2860                                 if (bss_size & (align - 1))
2861                                         bss_size = (bss_size | (align - 1)) + 1;
2862
2863                                 c->sym->secidx = i;
2864                                 c->sym->value = bss_size;
2865
2866                                 bss_size += c->sym->size;
2867                         }
2868
2869                         f->sections[i]->header.sh_size = bss_size;
2870                         f->sections[i]->header.sh_addralign = max_align;
2871                 }
2872         }
2873
2874         /* For the sake of patch relocation and parameter initialization,
2875            allocate zeroed data for NOBITS sections now.  Note that after
2876            this we cannot assume NOBITS are really empty.  */
2877         for (i = 0; i < f->header.e_shnum; ++i) {
2878                 struct obj_section *s = f->sections[i];
2879                 if (s->header.sh_type == SHT_NOBITS) {
2880                         if (s->header.sh_size != 0)
2881                         s->contents = memset(xmalloc(s->header.sh_size),
2882                                                                  0, s->header.sh_size);
2883                         else
2884                                 s->contents = NULL;
2885
2886                         s->header.sh_type = SHT_PROGBITS;
2887                 }
2888         }
2889 }
2890
2891 static unsigned long obj_load_size(struct obj_file *f)
2892 {
2893         unsigned long dot = 0;
2894         struct obj_section *sec;
2895
2896         /* Finalize the positions of the sections relative to one another.  */
2897
2898         for (sec = f->load_order; sec; sec = sec->load_next) {
2899                 ElfW(Addr) align;
2900
2901                 align = sec->header.sh_addralign;
2902                 if (align && (dot & (align - 1)))
2903                         dot = (dot | (align - 1)) + 1;
2904
2905                 sec->header.sh_addr = dot;
2906                 dot += sec->header.sh_size;
2907         }
2908
2909         return dot;
2910 }
2911
2912 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2913 {
2914         int i, n = f->header.e_shnum;
2915         int ret = 1;
2916
2917         /* Finalize the addresses of the sections.  */
2918
2919         f->baseaddr = base;
2920         for (i = 0; i < n; ++i)
2921                 f->sections[i]->header.sh_addr += base;
2922
2923         /* And iterate over all of the relocations.  */
2924
2925         for (i = 0; i < n; ++i) {
2926                 struct obj_section *relsec, *symsec, *targsec, *strsec;
2927                 ElfW(RelM) * rel, *relend;
2928                 ElfW(Sym) * symtab;
2929                 const char *strtab;
2930
2931                 relsec = f->sections[i];
2932                 if (relsec->header.sh_type != SHT_RELM)
2933                         continue;
2934
2935                 symsec = f->sections[relsec->header.sh_link];
2936                 targsec = f->sections[relsec->header.sh_info];
2937                 strsec = f->sections[symsec->header.sh_link];
2938
2939                 rel = (ElfW(RelM) *) relsec->contents;
2940                 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2941                 symtab = (ElfW(Sym) *) symsec->contents;
2942                 strtab = (const char *) strsec->contents;
2943
2944                 for (; rel < relend; ++rel) {
2945                         ElfW(Addr) value = 0;
2946                         struct obj_symbol *intsym = NULL;
2947                         unsigned long symndx;
2948                         ElfW(Sym) * extsym = 0;
2949                         const char *errmsg;
2950
2951                         /* Attempt to find a value to use for this relocation.  */
2952
2953                         symndx = ELFW(R_SYM) (rel->r_info);
2954                         if (symndx) {
2955                                 /* Note we've already checked for undefined symbols.  */
2956
2957                                 extsym = &symtab[symndx];
2958                                 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2959                                         /* Local symbols we look up in the local table to be sure
2960                                            we get the one that is really intended.  */
2961                                         intsym = f->local_symtab[symndx];
2962                                 } else {
2963                                         /* Others we look up in the hash table.  */
2964                                         const char *name;
2965                                         if (extsym->st_name)
2966                                                 name = strtab + extsym->st_name;
2967                                         else
2968                                                 name = f->sections[extsym->st_shndx]->name;
2969                                         intsym = obj_find_symbol(f, name);
2970                                 }
2971
2972                                 value = obj_symbol_final_value(f, intsym);
2973                                 intsym->referenced = 1;
2974                         }
2975 #if SHT_RELM == SHT_RELA
2976 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2977                         /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9.  */
2978                         if (!extsym || !extsym->st_name ||
2979                                 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2980 #endif
2981                                 value += rel->r_addend;
2982 #endif
2983
2984                         /* Do it! */
2985                         switch (arch_apply_relocation
2986                                         (f, targsec, symsec, intsym, rel, value)) {
2987                         case obj_reloc_ok:
2988                                 break;
2989
2990                         case obj_reloc_overflow:
2991                                 errmsg = "Relocation overflow";
2992                                 goto bad_reloc;
2993                         case obj_reloc_dangerous:
2994                                 errmsg = "Dangerous relocation";
2995                                 goto bad_reloc;
2996                         case obj_reloc_unhandled:
2997                                 errmsg = "Unhandled relocation";
2998                           bad_reloc:
2999                                 if (extsym) {
3000                                         error_msg("%s of type %ld for %s", errmsg,
3001                                                         (long) ELFW(R_TYPE) (rel->r_info),
3002                                                         strtab + extsym->st_name);
3003                                 } else {
3004                                         error_msg("%s of type %ld", errmsg,
3005                                                         (long) ELFW(R_TYPE) (rel->r_info));
3006                                 }
3007                                 ret = 0;
3008                                 break;
3009                         }
3010                 }
3011         }
3012
3013         /* Finally, take care of the patches.  */
3014
3015         if (f->string_patches) {
3016                 struct obj_string_patch *p;
3017                 struct obj_section *strsec;
3018                 ElfW(Addr) strsec_base;
3019                 strsec = obj_find_section(f, ".kstrtab");
3020                 strsec_base = strsec->header.sh_addr;
3021
3022                 for (p = f->string_patches; p; p = p->next) {
3023                         struct obj_section *targsec = f->sections[p->reloc_secidx];
3024                         *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3025                                 = strsec_base + p->string_offset;
3026                 }
3027         }
3028
3029         if (f->symbol_patches) {
3030                 struct obj_symbol_patch *p;
3031
3032                 for (p = f->symbol_patches; p; p = p->next) {
3033                         struct obj_section *targsec = f->sections[p->reloc_secidx];
3034                         *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3035                                 = obj_symbol_final_value(f, p->sym);
3036                 }
3037         }
3038
3039         return ret;
3040 }
3041
3042 static int obj_create_image(struct obj_file *f, char *image)
3043 {
3044         struct obj_section *sec;
3045         ElfW(Addr) base = f->baseaddr;
3046
3047         for (sec = f->load_order; sec; sec = sec->load_next) {
3048                 char *secimg;
3049
3050                 if (sec->contents == 0 || sec->header.sh_size == 0)
3051                         continue;
3052
3053                 secimg = image + (sec->header.sh_addr - base);
3054
3055                 /* Note that we allocated data for NOBITS sections earlier.  */
3056                 memcpy(secimg, sec->contents, sec->header.sh_size);
3057         }
3058
3059         return 1;
3060 }
3061
3062 /*======================================================================*/
3063
3064 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3065 {
3066         struct obj_file *f;
3067         ElfW(Shdr) * section_headers;
3068         int shnum, i;
3069         char *shstrtab;
3070
3071         /* Read the file header.  */
3072
3073         f = arch_new_file();
3074         memset(f, 0, sizeof(*f));
3075         f->symbol_cmp = strcmp;
3076         f->symbol_hash = obj_elf_hash;
3077         f->load_order_search_start = &f->load_order;
3078
3079         fseek(fp, 0, SEEK_SET);
3080         if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3081                 perror_msg("error reading ELF header");
3082                 return NULL;
3083         }
3084
3085         if (f->header.e_ident[EI_MAG0] != ELFMAG0
3086                 || f->header.e_ident[EI_MAG1] != ELFMAG1
3087                 || f->header.e_ident[EI_MAG2] != ELFMAG2
3088                 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3089                 error_msg("not an ELF file");
3090                 return NULL;
3091         }
3092         if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3093                 || f->header.e_ident[EI_DATA] != ELFDATAM
3094                 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3095                 || !MATCH_MACHINE(f->header.e_machine)) {
3096                 error_msg("ELF file not for this architecture");
3097                 return NULL;
3098         }
3099         if (f->header.e_type != ET_REL) {
3100                 error_msg("ELF file not a relocatable object");
3101                 return NULL;
3102         }
3103
3104         /* Read the section headers.  */
3105
3106         if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3107                 error_msg("section header size mismatch: %lu != %lu",
3108                                 (unsigned long) f->header.e_shentsize,
3109                                 (unsigned long) sizeof(ElfW(Shdr)));
3110                 return NULL;
3111         }
3112
3113         shnum = f->header.e_shnum;
3114         f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3115         memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3116
3117         section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3118         fseek(fp, f->header.e_shoff, SEEK_SET);
3119         if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3120                 perror_msg("error reading ELF section headers");
3121                 return NULL;
3122         }
3123
3124         /* Read the section data.  */
3125
3126         for (i = 0; i < shnum; ++i) {
3127                 struct obj_section *sec;
3128
3129                 f->sections[i] = sec = arch_new_section();
3130                 memset(sec, 0, sizeof(*sec));
3131
3132                 sec->header = section_headers[i];
3133                 sec->idx = i;
3134
3135                 if(sec->header.sh_size) switch (sec->header.sh_type) {
3136                 case SHT_NULL:
3137                 case SHT_NOTE:
3138                 case SHT_NOBITS:
3139                         /* ignore */
3140                         break;
3141
3142                 case SHT_PROGBITS:
3143 #if LOADBITS
3144                         if (!loadprogbits) {
3145                                 sec->contents = NULL;
3146                                 break;
3147                         }
3148 #endif                  
3149                 case SHT_SYMTAB:
3150                 case SHT_STRTAB:
3151                 case SHT_RELM:
3152                         if (sec->header.sh_size > 0) {
3153                                 sec->contents = xmalloc(sec->header.sh_size);
3154                                 fseek(fp, sec->header.sh_offset, SEEK_SET);
3155                                 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3156                                         perror_msg("error reading ELF section data");
3157                                         return NULL;
3158                                 }
3159                         } else {
3160                                 sec->contents = NULL;
3161                         }
3162                         break;
3163
3164 #if SHT_RELM == SHT_REL
3165                 case SHT_RELA:
3166                         error_msg("RELA relocations not supported on this architecture");
3167                         return NULL;
3168 #else
3169                 case SHT_REL:
3170                         error_msg("REL relocations not supported on this architecture");
3171                         return NULL;
3172 #endif
3173
3174                 default:
3175                         if (sec->header.sh_type >= SHT_LOPROC) {
3176                                 /* Assume processor specific section types are debug
3177                                    info and can safely be ignored.  If this is ever not
3178                                    the case (Hello MIPS?), don't put ifdefs here but
3179                                    create an arch_load_proc_section().  */
3180                                 break;
3181                         }
3182
3183                         error_msg("can't handle sections of type %ld",
3184                                         (long) sec->header.sh_type);
3185                         return NULL;
3186                 }
3187         }
3188
3189         /* Do what sort of interpretation as needed by each section.  */
3190
3191         shstrtab = f->sections[f->header.e_shstrndx]->contents;
3192
3193         for (i = 0; i < shnum; ++i) {
3194                 struct obj_section *sec = f->sections[i];
3195                 sec->name = shstrtab + sec->header.sh_name;
3196         }
3197
3198         for (i = 0; i < shnum; ++i) {
3199                 struct obj_section *sec = f->sections[i];
3200
3201                 /* .modinfo should be contents only but gcc has no attribute for that.
3202                  * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3203                  */
3204                 if (strcmp(sec->name, ".modinfo") == 0)
3205                         sec->header.sh_flags &= ~SHF_ALLOC;
3206
3207                 if (sec->header.sh_flags & SHF_ALLOC)
3208                         obj_insert_section_load_order(f, sec);
3209
3210                 switch (sec->header.sh_type) {
3211                 case SHT_SYMTAB:
3212                         {
3213                                 unsigned long nsym, j;
3214                                 char *strtab;
3215                                 ElfW(Sym) * sym;
3216
3217                                 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3218                                         error_msg("symbol size mismatch: %lu != %lu",
3219                                                         (unsigned long) sec->header.sh_entsize,
3220                                                         (unsigned long) sizeof(ElfW(Sym)));
3221                                         return NULL;
3222                                 }
3223
3224                                 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3225                                 strtab = f->sections[sec->header.sh_link]->contents;
3226                                 sym = (ElfW(Sym) *) sec->contents;
3227
3228                                 /* Allocate space for a table of local symbols.  */
3229                                 j = f->local_symtab_size = sec->header.sh_info;
3230                                 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3231
3232                                 /* Insert all symbols into the hash table.  */
3233                                 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3234                                         const char *name;
3235                                         if (sym->st_name)
3236                                                 name = strtab + sym->st_name;
3237                                         else
3238                                                 name = f->sections[sym->st_shndx]->name;
3239
3240                                         obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3241                                                                    sym->st_value, sym->st_size);
3242                                 }
3243                         }
3244                         break;
3245
3246                 case SHT_RELM:
3247                         if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3248                                 error_msg("relocation entry size mismatch: %lu != %lu",
3249                                                 (unsigned long) sec->header.sh_entsize,
3250                                                 (unsigned long) sizeof(ElfW(RelM)));
3251                                 return NULL;
3252                         }
3253                         break;
3254                         /* XXX  Relocation code from modutils-2.3.19 is not here.
3255                          * Why?  That's about 20 lines of code from obj/obj_load.c,
3256                          * which gets done in a second pass through the sections.
3257                          * This BusyBox insmod does similar work in obj_relocate(). */
3258                 }
3259         }
3260
3261         return f;
3262 }
3263
3264 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3265 /*
3266  * load the unloaded sections directly into the memory allocated by
3267  * kernel for the module
3268  */
3269
3270 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3271 {
3272         ElfW(Addr) base = f->baseaddr;
3273         struct obj_section* sec;
3274         
3275         for (sec = f->load_order; sec; sec = sec->load_next) {
3276
3277                 /* section already loaded? */
3278                 if (sec->contents != NULL)
3279                         continue;
3280                 
3281                 if (sec->header.sh_size == 0)
3282                         continue;
3283
3284                 sec->contents = imagebase + (sec->header.sh_addr - base);
3285                 fseek(fp, sec->header.sh_offset, SEEK_SET);
3286                 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3287                         error_msg("error reading ELF section data: %s\n", strerror(errno));
3288                         return 0;
3289                 }
3290
3291         }
3292         return 1;
3293 }
3294 #endif
3295
3296 static void hide_special_symbols(struct obj_file *f)
3297 {
3298         static const char *const specials[] = {
3299                 SPFX "cleanup_module",
3300                 SPFX "init_module",
3301                 SPFX "kernel_version",
3302                 NULL
3303         };
3304
3305         struct obj_symbol *sym;
3306         const char *const *p;
3307
3308         for (p = specials; *p; ++p)
3309                 if ((sym = obj_find_symbol(f, *p)) != NULL)
3310                         sym->info =
3311                                 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3312 }
3313
3314
3315
3316 extern int insmod_main( int argc, char **argv)
3317 {
3318         int opt;
3319         int k_crcs;
3320         int k_new_syscalls;
3321         int len;
3322         char *tmp;
3323         unsigned long m_size;
3324         ElfW(Addr) m_addr;
3325         FILE *fp;
3326         struct obj_file *f;
3327         struct stat st;
3328         char m_name[FILENAME_MAX + 1] = "\0";
3329         int exit_status = EXIT_FAILURE;
3330         int m_has_modinfo;
3331 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3332         struct utsname uts_info;
3333         char m_strversion[STRVERSIONLEN];
3334         int m_version;
3335         int m_crcs;
3336 #endif
3337
3338         /* Parse any options */
3339         while ((opt = getopt(argc, argv, "fksvxLo:")) > 0) {
3340                 switch (opt) {
3341                         case 'f':                       /* force loading */
3342                                 flag_force_load = 1;
3343                                 break;
3344                         case 'k':                       /* module loaded by kerneld, auto-cleanable */
3345                                 flag_autoclean = 1;
3346                                 break;
3347                         case 's':                       /* log to syslog */
3348                                 /* log to syslog -- not supported              */
3349                                 /* but kernel needs this for request_module(), */
3350                                 /* as this calls: modprobe -k -s -- <module>   */
3351                                 /* so silently ignore this flag                */
3352                                 break;
3353                         case 'v':                       /* verbose output */
3354                                 flag_verbose = 1;
3355                                 break;
3356                         case 'x':                       /* do not export externs */
3357                                 flag_export = 0;
3358                                 break;
3359                         case 'o':                       /* name the output module */
3360                                 strncpy(m_name, optarg, FILENAME_MAX);
3361                                 break;
3362                         case 'L':                       /* Stub warning */
3363                                 /* This is needed for compatibility with modprobe.
3364                                  * In theory, this does locking, but we don't do
3365                                  * that.  So be careful and plan your life around not
3366                                  * loading the same module 50 times concurrently. */
3367                                 break;
3368                         default:
3369                                 show_usage();
3370                 }
3371         }
3372         
3373         if (argv[optind] == NULL) {
3374                 show_usage();
3375         }
3376
3377         /* Grab the module name */
3378         if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3379                 tmp++;
3380         } else {
3381                 tmp = argv[optind];
3382         }
3383         len = strlen(tmp);
3384
3385         if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3386                 len -= 2;
3387         memcpy(m_fullName, tmp, len);
3388         m_fullName[len]='\0';
3389         if (*m_name == '\0') {
3390                 strcpy(m_name, m_fullName);
3391         }
3392         strcat(m_fullName, ".o");
3393
3394         /* Get a filedesc for the module.  Check we we have a complete path */
3395         if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3396                         (fp = fopen(argv[optind], "r")) == NULL) {
3397                 struct utsname myuname;
3398
3399                 /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
3400                  * but do not error out yet if we fail to find it... */
3401                 if (uname(&myuname) == 0) {
3402                         char module_dir[FILENAME_MAX];
3403                         char real_module_dir[FILENAME_MAX];
3404                         snprintf (module_dir, sizeof(module_dir), "%s/%s", 
3405                                         _PATH_MODULES, myuname.release);
3406                         /* Jump through hoops in case /lib/modules/`uname -r`
3407                          * is a symlink.  We do not want recursive_action to
3408                          * follow symlinks, but we do want to follow the
3409                          * /lib/modules/`uname -r` dir, So resolve it ourselves
3410                          * if it is a link... */
3411                         if (realpath (module_dir, real_module_dir) == NULL)
3412                                 strcpy(real_module_dir, module_dir);
3413                         recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3414                                         check_module_name_match, 0, m_fullName);
3415                 }
3416
3417                 /* Check if we have found anything yet */
3418                 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL)) 
3419                 {
3420                         char module_dir[FILENAME_MAX];
3421                         if (realpath (_PATH_MODULES, module_dir) == NULL)
3422                                 strcpy(module_dir, _PATH_MODULES);
3423                         /* No module found under /lib/modules/`uname -r`, this
3424                          * time cast the net a bit wider.  Search /lib/modules/ */
3425                         if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3426                                                 check_module_name_match, 0, m_fullName)) 
3427                         {
3428                                 if (m_filename[0] == '\0'
3429                                                 || ((fp = fopen(m_filename, "r")) == NULL)) 
3430                                 {
3431                                         error_msg("%s: no module by that name found", m_fullName);
3432                                         return EXIT_FAILURE;
3433                                 }
3434                         } else
3435                                 error_msg_and_die("%s: no module by that name found", m_fullName);
3436                 }
3437         } else 
3438                 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3439
3440         printf("Using %s\n", m_filename);
3441
3442         if ((f = obj_load(fp, LOADBITS)) == NULL)
3443                 perror_msg_and_die("Could not load the module");
3444
3445         if (get_modinfo_value(f, "kernel_version") == NULL)
3446                 m_has_modinfo = 0;
3447         else
3448                 m_has_modinfo = 1;
3449
3450 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3451         /* Version correspondence?  */
3452
3453         if (uname(&uts_info) < 0)
3454                 uts_info.release[0] = '\0';
3455         if (m_has_modinfo) {
3456                 m_version = new_get_module_version(f, m_strversion);
3457         } else {
3458                 m_version = old_get_module_version(f, m_strversion);
3459                 if (m_version == -1) {
3460                         error_msg("couldn't find the kernel version the module was "
3461                                         "compiled for");
3462                         goto out;
3463                 }
3464         }
3465
3466         if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3467                 if (flag_force_load) {
3468                         error_msg("Warning: kernel-module version mismatch\n"
3469                                         "\t%s was compiled for kernel version %s\n"
3470                                         "\twhile this kernel is version %s",
3471                                         m_filename, m_strversion, uts_info.release);
3472                 } else {
3473                         error_msg("kernel-module version mismatch\n"
3474                                         "\t%s was compiled for kernel version %s\n"
3475                                         "\twhile this kernel is version %s.",
3476                                         m_filename, m_strversion, uts_info.release);
3477                         goto out;
3478                 }
3479         }
3480         k_crcs = 0;
3481 #endif                                                  /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3482
3483         k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3484
3485         if (k_new_syscalls) {
3486 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3487                 if (!new_get_kernel_symbols())
3488                         goto out;
3489                 k_crcs = new_is_kernel_checksummed();
3490 #else
3491                 error_msg("Not configured to support new kernels");
3492                 goto out;
3493 #endif
3494         } else {
3495 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
3496                 if (!old_get_kernel_symbols(m_name))
3497                         goto out;
3498                 k_crcs = old_is_kernel_checksummed();
3499 #else
3500                 error_msg("Not configured to support old kernels");
3501                 goto out;
3502 #endif
3503         }
3504
3505 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3506         if (m_has_modinfo)
3507                 m_crcs = new_is_module_checksummed(f);
3508         else
3509                 m_crcs = old_is_module_checksummed(f);
3510
3511         if (m_crcs != k_crcs)
3512                 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3513 #endif                                                  /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3514
3515         /* Let the module know about the kernel symbols.  */
3516         add_kernel_symbols(f);
3517
3518         /* Allocate common symbols, symbol tables, and string tables.  */
3519
3520         if (k_new_syscalls 
3521                 ? !new_create_this_module(f, m_name)
3522                 : !old_create_mod_use_count(f)) 
3523         {
3524                 goto out;
3525         }
3526
3527         if (!obj_check_undefineds(f)) {
3528                 goto out;
3529         }
3530         obj_allocate_commons(f);
3531
3532         /* done with the module name, on to the optional var=value arguments */
3533         ++optind;
3534
3535         if (optind < argc) {
3536                 if (m_has_modinfo
3537                         ? !new_process_module_arguments(f, argc - optind, argv + optind) 
3538                         : !old_process_module_arguments(f, argc - optind, argv + optind)) 
3539                 {
3540                         goto out;
3541                 }
3542         }
3543
3544         arch_create_got(f);
3545         hide_special_symbols(f);
3546
3547         if (k_new_syscalls)
3548                 new_create_module_ksymtab(f);
3549
3550         /* Find current size of the module */
3551         m_size = obj_load_size(f);
3552
3553
3554         m_addr = create_module(m_name, m_size);
3555         if (m_addr==-1) switch (errno) {
3556         case EEXIST:
3557                 error_msg("A module named %s already exists", m_name);
3558                 goto out;
3559         case ENOMEM:
3560                 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3561                                 m_size);
3562                 goto out;
3563         default:
3564                 perror_msg("create_module: %s", m_name);
3565                 goto out;
3566         }
3567
3568 #if  !LOADBITS
3569         /*
3570          * the PROGBITS section was not loaded by the obj_load
3571          * now we can load them directly into the kernel memory
3572          */
3573         if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3574                 delete_module(m_name);
3575                 goto out;
3576         }
3577 #endif  
3578
3579         if (!obj_relocate(f, m_addr)) {
3580                 delete_module(m_name);
3581                 goto out;
3582         }
3583
3584         if (k_new_syscalls 
3585                 ? !new_init_module(m_name, f, m_size)
3586                 : !old_init_module(m_name, f, m_size)) 
3587         {
3588                 delete_module(m_name);
3589                 goto out;
3590         }
3591
3592         exit_status = EXIT_SUCCESS;
3593
3594 out:
3595         fclose(fp);
3596         return(exit_status);
3597 }