Add/improve comment, shorten some usage texts. No code changes.
[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 ARM, CRIS, H8/300, x86, ia64, x86_64,
6  * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
7  *
8  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
9  * and Ron Alder <alder@lineo.com>
10  *
11  * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
12  *   Added x86_64 support.
13  *
14  * Miles Bader <miles@gnu.org> added NEC V850E support.
15  *
16  * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
17  * and (theoretically) SH3. I have only tested SH4 in little endian mode.
18  *
19  * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
20  * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI.  Only
21  * very minor changes required to also work with StrongArm and presumably
22  * all ARM based systems.
23  *
24  * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
25  *   added Renesas H8/300 support.
26  *
27  * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
28  *   Integrated support for sh64 (SH-5), from preliminary modutils
29  *   patches from Benedict Gaster <benedict.gaster@superh.com>.
30  *   Currently limited to support for 32bit ABI.
31  *
32  * Magnus Damm <damm@opensource.se> 22-May-2002.
33  *   The plt and got code are now using the same structs.
34  *   Added generic linked list code to fully support PowerPC.
35  *   Replaced the mess in arch_apply_relocation() with architecture blocks.
36  *   The arch_create_got() function got cleaned up with architecture blocks.
37  *   These blocks should be easy maintain and sync with obj_xxx.c in modutils.
38  *
39  * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
40  *   PowerPC specific code stolen from modutils-2.3.16,
41  *   written by Paul Mackerras, Copyright 1996, 1997 Linux International.
42  *   I've only tested the code on mpc8xx platforms in big-endian mode.
43  *   Did some cleanup and added USE_xxx_ENTRIES...
44  *
45  * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
46  *   based on modutils-2.4.2
47  *   MIPS specific support for Elf loading and relocation.
48  *   Copyright 1996, 1997 Linux International.
49  *   Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
50  *
51  * Based almost entirely on the Linux modutils-2.3.11 implementation.
52  *   Copyright 1996, 1997 Linux International.
53  *   New implementation contributed by Richard Henderson <rth@tamu.edu>
54  *   Based on original work by Bjorn Ekwall <bj0rn@blox.se>
55  *   Restructured (and partly rewritten) by:
56  *   Björn Ekwall <bj0rn@blox.se> February 1999
57  *
58  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
59  */
60
61 #include "busybox.h"
62 #include <libgen.h>
63 #include <sys/utsname.h>
64
65 #if !ENABLE_FEATURE_2_4_MODULES && !ENABLE_FEATURE_2_6_MODULES
66 #undef ENABLE_FEATURE_2_4_MODULES
67 #define ENABLE_FEATURE_2_4_MODULES 1
68 #endif
69
70 #if !ENABLE_FEATURE_2_4_MODULES
71 #define insmod_ng_main insmod_main
72 #endif
73
74 #if ENABLE_FEATURE_2_6_MODULES
75 extern int insmod_ng_main( int argc, char **argv);
76 #endif
77
78
79 #if ENABLE_FEATURE_2_4_MODULES
80
81
82 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
83 #define LOADBITS 0
84 #else
85 #define LOADBITS 1
86 #endif
87
88
89 /* Alpha */
90 #if defined(__alpha__)
91 #define MATCH_MACHINE(x) (x == EM_ALPHA)
92 #define SHT_RELM       SHT_RELA
93 #define Elf64_RelM     Elf64_Rela
94 #define ELFCLASSM      ELFCLASS64
95 #endif
96
97 /* ARM support */
98 #if defined(__arm__)
99 #define MATCH_MACHINE(x) (x == EM_ARM)
100 #define SHT_RELM        SHT_REL
101 #define Elf32_RelM      Elf32_Rel
102 #define ELFCLASSM       ELFCLASS32
103 #define USE_PLT_ENTRIES
104 #define PLT_ENTRY_SIZE 8
105 #define USE_GOT_ENTRIES
106 #define GOT_ENTRY_SIZE 8
107 #define USE_SINGLE
108 #endif
109
110 /* blackfin */
111 #if defined(BFIN)
112 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
113 #define SHT_RELM        SHT_RELA
114 #define Elf32_RelM      Elf32_Rela
115 #define ELFCLASSM       ELFCLASS32
116 #endif
117
118 /* CRIS */
119 #if defined(__cris__)
120 #define MATCH_MACHINE(x) (x == EM_CRIS)
121 #define SHT_RELM        SHT_RELA
122 #define Elf32_RelM      Elf32_Rela
123 #define ELFCLASSM       ELFCLASS32
124 #ifndef EM_CRIS
125 #define EM_CRIS 76
126 #define R_CRIS_NONE 0
127 #define R_CRIS_32   3
128 #endif
129 #endif
130
131 /* H8/300 */
132 #if defined(__H8300H__) || defined(__H8300S__)
133 #define MATCH_MACHINE(x) (x == EM_H8_300)
134 #define SHT_RELM        SHT_RELA
135 #define Elf32_RelM      Elf32_Rela
136 #define ELFCLASSM       ELFCLASS32
137 #define USE_SINGLE
138 #define SYMBOL_PREFIX   "_"
139 #endif
140
141 /* PA-RISC / HP-PA */
142 #if defined(__hppa__)
143 #define MATCH_MACHINE(x) (x == EM_PARISC)
144 #define SHT_RELM       SHT_RELA
145 #if defined(__LP64__)
146 #define Elf64_RelM     Elf64_Rela
147 #define ELFCLASSM      ELFCLASS64
148 #else
149 #define Elf32_RelM     Elf32_Rela
150 #define ELFCLASSM      ELFCLASS32
151 #endif
152 #endif
153
154 /* x86 */
155 #if defined(__i386__)
156 #ifndef EM_486
157 #define MATCH_MACHINE(x) (x == EM_386)
158 #else
159 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
160 #endif
161 #define SHT_RELM        SHT_REL
162 #define Elf32_RelM      Elf32_Rel
163 #define ELFCLASSM       ELFCLASS32
164 #define USE_GOT_ENTRIES
165 #define GOT_ENTRY_SIZE 4
166 #define USE_SINGLE
167 #endif
168
169 /* IA64, aka Itanium */
170 #if defined(__ia64__)
171 #define MATCH_MACHINE(x) (x == EM_IA_64)
172 #define SHT_RELM       SHT_RELA
173 #define Elf64_RelM     Elf64_Rela
174 #define ELFCLASSM      ELFCLASS64
175 #endif
176
177 /* m68k */
178 #if defined(__mc68000__)
179 #define MATCH_MACHINE(x) (x == EM_68K)
180 #define SHT_RELM        SHT_RELA
181 #define Elf32_RelM      Elf32_Rela
182 #define ELFCLASSM       ELFCLASS32
183 #define USE_GOT_ENTRIES
184 #define GOT_ENTRY_SIZE 4
185 #define USE_SINGLE
186 #endif
187
188 /* Microblaze */
189 #if defined(__microblaze__)
190 #define USE_SINGLE
191 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
192 #define SHT_RELM        SHT_RELA
193 #define Elf32_RelM      Elf32_Rela
194 #define ELFCLASSM       ELFCLASS32
195 #endif
196
197 /* MIPS */
198 #if defined(__mips__)
199 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
200 #define SHT_RELM        SHT_REL
201 #define Elf32_RelM      Elf32_Rel
202 #define ELFCLASSM       ELFCLASS32
203 /* Account for ELF spec changes.  */
204 #ifndef EM_MIPS_RS3_LE
205 #ifdef EM_MIPS_RS4_BE
206 #define EM_MIPS_RS3_LE  EM_MIPS_RS4_BE
207 #else
208 #define EM_MIPS_RS3_LE  10
209 #endif
210 #endif /* !EM_MIPS_RS3_LE */
211 #define ARCHDATAM       "__dbe_table"
212 #endif
213
214 /* Nios II */
215 #if defined(__nios2__)
216 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
217 #define SHT_RELM        SHT_RELA
218 #define Elf32_RelM      Elf32_Rela
219 #define ELFCLASSM       ELFCLASS32
220 #endif
221
222 /* PowerPC */
223 #if defined(__powerpc64__)
224 #define MATCH_MACHINE(x) (x == EM_PPC64)
225 #define SHT_RELM        SHT_RELA
226 #define Elf64_RelM      Elf64_Rela
227 #define ELFCLASSM       ELFCLASS64
228 #elif defined(__powerpc__)
229 #define MATCH_MACHINE(x) (x == EM_PPC)
230 #define SHT_RELM        SHT_RELA
231 #define Elf32_RelM      Elf32_Rela
232 #define ELFCLASSM       ELFCLASS32
233 #define USE_PLT_ENTRIES
234 #define PLT_ENTRY_SIZE 16
235 #define USE_PLT_LIST
236 #define LIST_ARCHTYPE ElfW(Addr)
237 #define USE_LIST
238 #define ARCHDATAM       "__ftr_fixup"
239 #endif
240
241 /* S390 */
242 #if defined(__s390__)
243 #define MATCH_MACHINE(x) (x == EM_S390)
244 #define SHT_RELM        SHT_RELA
245 #define Elf32_RelM      Elf32_Rela
246 #define ELFCLASSM       ELFCLASS32
247 #define USE_PLT_ENTRIES
248 #define PLT_ENTRY_SIZE 8
249 #define USE_GOT_ENTRIES
250 #define GOT_ENTRY_SIZE 8
251 #define USE_SINGLE
252 #endif
253
254 /* SuperH */
255 #if defined(__sh__)
256 #define MATCH_MACHINE(x) (x == EM_SH)
257 #define SHT_RELM        SHT_RELA
258 #define Elf32_RelM      Elf32_Rela
259 #define ELFCLASSM       ELFCLASS32
260 #define USE_GOT_ENTRIES
261 #define GOT_ENTRY_SIZE 4
262 #define USE_SINGLE
263 /* the SH changes have only been tested in =little endian= mode */
264 /* I'm not sure about big endian, so let's warn: */
265 #if defined(__sh__) && BB_BIG_ENDIAN
266 # error insmod.c may require changes for use on big endian SH
267 #endif
268 /* it may or may not work on the SH1/SH2... Error on those also */
269 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
270 #error insmod.c may require changes for SH1 or SH2 use
271 #endif
272 #endif
273
274 /* Sparc */
275 #if defined(__sparc__)
276 #define MATCH_MACHINE(x) (x == EM_SPARC)
277 #define SHT_RELM       SHT_RELA
278 #define Elf32_RelM     Elf32_Rela
279 #define ELFCLASSM      ELFCLASS32
280 #endif
281
282 /* v850e */
283 #if defined (__v850e__)
284 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
285 #define SHT_RELM        SHT_RELA
286 #define Elf32_RelM      Elf32_Rela
287 #define ELFCLASSM       ELFCLASS32
288 #define USE_PLT_ENTRIES
289 #define PLT_ENTRY_SIZE 8
290 #define USE_SINGLE
291 #ifndef EM_CYGNUS_V850  /* grumble */
292 #define EM_CYGNUS_V850  0x9080
293 #endif
294 #define SYMBOL_PREFIX   "_"
295 #endif
296
297 /* X86_64  */
298 #if defined(__x86_64__)
299 #define MATCH_MACHINE(x) (x == EM_X86_64)
300 #define SHT_RELM        SHT_RELA
301 #define USE_GOT_ENTRIES
302 #define GOT_ENTRY_SIZE 8
303 #define USE_SINGLE
304 #define Elf64_RelM      Elf64_Rela
305 #define ELFCLASSM       ELFCLASS64
306 #endif
307
308 #ifndef SHT_RELM
309 #error Sorry, but insmod.c does not yet support this architecture...
310 #endif
311
312
313 //----------------------------------------------------------------------------
314 //--------modutils module.h, lines 45-242
315 //----------------------------------------------------------------------------
316
317 /* Definitions for the Linux module syscall interface.
318    Copyright 1996, 1997 Linux International.
319
320    Contributed by Richard Henderson <rth@tamu.edu>
321
322    This file is part of the Linux modutils.
323
324    This program is free software; you can redistribute it and/or modify it
325    under the terms of the GNU General Public License as published by the
326    Free Software Foundation; either version 2 of the License, or (at your
327    option) any later version.
328
329    This program is distributed in the hope that it will be useful, but
330    WITHOUT ANY WARRANTY; without even the implied warranty of
331    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
332    General Public License for more details.
333
334    You should have received a copy of the GNU General Public License
335    along with this program; if not, write to the Free Software Foundation,
336    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
337
338
339 #ifndef MODUTILS_MODULE_H
340
341 /*======================================================================*/
342 /* For sizeof() which are related to the module platform and not to the
343    environment isnmod is running in, use sizeof_xx instead of sizeof(xx).  */
344
345 #define tgt_sizeof_char         sizeof(char)
346 #define tgt_sizeof_short        sizeof(short)
347 #define tgt_sizeof_int          sizeof(int)
348 #define tgt_sizeof_long         sizeof(long)
349 #define tgt_sizeof_char_p       sizeof(char *)
350 #define tgt_sizeof_void_p       sizeof(void *)
351 #define tgt_long                long
352
353 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
354 #undef tgt_sizeof_long
355 #undef tgt_sizeof_char_p
356 #undef tgt_sizeof_void_p
357 #undef tgt_long
358 enum {
359         tgt_sizeof_long = 8,
360         tgt_sizeof_char_p = 8,
361         tgt_sizeof_void_p = 8
362 };
363 #define tgt_long                long long
364 #endif
365
366 /*======================================================================*/
367 /* The structures used in Linux 2.1.  */
368
369 /* Note: new_module_symbol does not use tgt_long intentionally */
370 struct new_module_symbol {
371         unsigned long value;
372         unsigned long name;
373 };
374
375 struct new_module_persist;
376
377 struct new_module_ref {
378         unsigned tgt_long dep;          /* kernel addresses */
379         unsigned tgt_long ref;
380         unsigned tgt_long next_ref;
381 };
382
383 struct new_module {
384         unsigned tgt_long size_of_struct;       /* == sizeof(module) */
385         unsigned tgt_long next;
386         unsigned tgt_long name;
387         unsigned tgt_long size;
388
389         tgt_long usecount;
390         unsigned tgt_long flags;                /* AUTOCLEAN et al */
391
392         unsigned nsyms;
393         unsigned ndeps;
394
395         unsigned tgt_long syms;
396         unsigned tgt_long deps;
397         unsigned tgt_long refs;
398         unsigned tgt_long init;
399         unsigned tgt_long cleanup;
400         unsigned tgt_long ex_table_start;
401         unsigned tgt_long ex_table_end;
402 #ifdef __alpha__
403         unsigned tgt_long gp;
404 #endif
405         /* Everything after here is extension.  */
406         unsigned tgt_long persist_start;
407         unsigned tgt_long persist_end;
408         unsigned tgt_long can_unload;
409         unsigned tgt_long runsize;
410         const char *kallsyms_start;     /* All symbols for kernel debugging */
411         const char *kallsyms_end;
412         const char *archdata_start;     /* arch specific data for module */
413         const char *archdata_end;
414         const char *kernel_data;        /* Reserved for kernel internal use */
415 };
416
417 #ifdef ARCHDATAM
418 #define ARCHDATA_SEC_NAME ARCHDATAM
419 #else
420 #define ARCHDATA_SEC_NAME "__archdata"
421 #endif
422 #define KALLSYMS_SEC_NAME "__kallsyms"
423
424
425 struct new_module_info {
426         unsigned long addr;
427         unsigned long size;
428         unsigned long flags;
429         long usecount;
430 };
431
432 /* Bits of module.flags.  */
433 enum {
434         NEW_MOD_RUNNING = 1,
435         NEW_MOD_DELETED = 2,
436         NEW_MOD_AUTOCLEAN = 4,
437         NEW_MOD_VISITED = 8,
438         NEW_MOD_USED_ONCE = 16
439 };
440
441 int init_module(const char *name, const struct new_module *);
442 int query_module(const char *name, int which, void *buf,
443                 size_t bufsize, size_t *ret);
444
445 /* Values for query_module's which.  */
446 enum {
447         QM_MODULES = 1,
448         QM_DEPS = 2,
449         QM_REFS = 3,
450         QM_SYMBOLS = 4,
451         QM_INFO = 5
452 };
453
454 /*======================================================================*/
455 /* The system calls unchanged between 2.0 and 2.1.  */
456
457 unsigned long create_module(const char *, size_t);
458 int delete_module(const char *);
459
460
461 #endif /* module.h */
462
463 //----------------------------------------------------------------------------
464 //--------end of modutils module.h
465 //----------------------------------------------------------------------------
466
467
468
469 //----------------------------------------------------------------------------
470 //--------modutils obj.h, lines 253-462
471 //----------------------------------------------------------------------------
472
473 /* Elf object file loading and relocation routines.
474    Copyright 1996, 1997 Linux International.
475
476    Contributed by Richard Henderson <rth@tamu.edu>
477
478    This file is part of the Linux modutils.
479
480    This program is free software; you can redistribute it and/or modify it
481    under the terms of the GNU General Public License as published by the
482    Free Software Foundation; either version 2 of the License, or (at your
483    option) any later version.
484
485    This program is distributed in the hope that it will be useful, but
486    WITHOUT ANY WARRANTY; without even the implied warranty of
487    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
488    General Public License for more details.
489
490    You should have received a copy of the GNU General Public License
491    along with this program; if not, write to the Free Software Foundation,
492    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
493
494
495 #ifndef MODUTILS_OBJ_H
496
497 /* The relocatable object is manipulated using elfin types.  */
498
499 #include <stdio.h>
500 #include <elf.h>
501 #include <endian.h>
502
503 #ifndef ElfW
504 # if ELFCLASSM == ELFCLASS32
505 #  define ElfW(x)  Elf32_ ## x
506 #  define ELFW(x)  ELF32_ ## x
507 # else
508 #  define ElfW(x)  Elf64_ ## x
509 #  define ELFW(x)  ELF64_ ## x
510 # endif
511 #endif
512
513 /* For some reason this is missing from some ancient C libraries....  */
514 #ifndef ELF32_ST_INFO
515 # define ELF32_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
516 #endif
517
518 #ifndef ELF64_ST_INFO
519 # define ELF64_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
520 #endif
521
522 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
523 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
524 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
525 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
526 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
527
528 struct obj_string_patch;
529 struct obj_symbol_patch;
530
531 struct obj_section
532 {
533         ElfW(Shdr) header;
534         const char *name;
535         char *contents;
536         struct obj_section *load_next;
537         int idx;
538 };
539
540 struct obj_symbol
541 {
542         struct obj_symbol *next;        /* hash table link */
543         const char *name;
544         unsigned long value;
545         unsigned long size;
546         int secidx;                     /* the defining section index/module */
547         int info;
548         int ksymidx;                    /* for export to the kernel symtab */
549         int referenced;         /* actually used in the link */
550 };
551
552 /* Hardcode the hash table size.  We shouldn't be needing so many
553    symbols that we begin to degrade performance, and we get a big win
554    by giving the compiler a constant divisor.  */
555
556 #define HASH_BUCKETS  521
557
558 struct obj_file {
559         ElfW(Ehdr) header;
560         ElfW(Addr) baseaddr;
561         struct obj_section **sections;
562         struct obj_section *load_order;
563         struct obj_section **load_order_search_start;
564         struct obj_string_patch *string_patches;
565         struct obj_symbol_patch *symbol_patches;
566         int (*symbol_cmp)(const char *, const char *);
567         unsigned long (*symbol_hash)(const char *);
568         unsigned long local_symtab_size;
569         struct obj_symbol **local_symtab;
570         struct obj_symbol *symtab[HASH_BUCKETS];
571 };
572
573 enum obj_reloc {
574         obj_reloc_ok,
575         obj_reloc_overflow,
576         obj_reloc_dangerous,
577         obj_reloc_unhandled
578 };
579
580 struct obj_string_patch {
581         struct obj_string_patch *next;
582         int reloc_secidx;
583         ElfW(Addr) reloc_offset;
584         ElfW(Addr) string_offset;
585 };
586
587 struct obj_symbol_patch {
588         struct obj_symbol_patch *next;
589         int reloc_secidx;
590         ElfW(Addr) reloc_offset;
591         struct obj_symbol *sym;
592 };
593
594
595 /* Generic object manipulation routines.  */
596
597 static unsigned long obj_elf_hash(const char *);
598
599 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
600
601 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
602                                          const char *name);
603
604 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
605                                   struct obj_symbol *sym);
606
607 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
608 static void obj_set_symbol_compare(struct obj_file *f,
609                             int (*cmp)(const char *, const char *),
610                             unsigned long (*hash)(const char *));
611 #endif
612
613 static struct obj_section *obj_find_section (struct obj_file *f,
614                                            const char *name);
615
616 static void obj_insert_section_load_order (struct obj_file *f,
617                                     struct obj_section *sec);
618
619 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
620                                                 const char *name,
621                                                 unsigned long align,
622                                                 unsigned long size);
623
624 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
625                                                       const char *name,
626                                                       unsigned long align,
627                                                       unsigned long size);
628
629 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
630
631 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
632                      const char *string);
633
634 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
635                      struct obj_symbol *sym);
636
637 static int obj_check_undefineds(struct obj_file *f);
638
639 static void obj_allocate_commons(struct obj_file *f);
640
641 static unsigned long obj_load_size (struct obj_file *f);
642
643 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
644
645 static struct obj_file *obj_load(FILE *f, int loadprogbits);
646
647 static int obj_create_image (struct obj_file *f, char *image);
648
649 /* Architecture specific manipulation routines.  */
650
651 static struct obj_file *arch_new_file (void);
652
653 static struct obj_section *arch_new_section (void);
654
655 static struct obj_symbol *arch_new_symbol (void);
656
657 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
658                                       struct obj_section *targsec,
659                                       struct obj_section *symsec,
660                                       struct obj_symbol *sym,
661                                       ElfW(RelM) *rel, ElfW(Addr) value);
662
663 static void arch_create_got (struct obj_file *f);
664 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
665 static int obj_gpl_license(struct obj_file *f, const char **license);
666 #endif /* FEATURE_CHECK_TAINTED_MODULE */
667 #endif /* obj.h */
668 //----------------------------------------------------------------------------
669 //--------end of modutils obj.h
670 //----------------------------------------------------------------------------
671
672
673 /* SPFX is always a string, so it can be concatenated to string constants.  */
674 #ifdef SYMBOL_PREFIX
675 #define SPFX    SYMBOL_PREFIX
676 #else
677 #define SPFX    ""
678 #endif
679
680
681 #define _PATH_MODULES   "/lib/modules"
682 enum { STRVERSIONLEN = 64 };
683
684 /*======================================================================*/
685
686 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
687 enum {
688         OPT_s = 0x1, // -s /* log to syslog */
689                 /* Not supported but kernel needs this for request_module(),
690                    as this calls: modprobe -k -s -- <module>
691                    so silently ignore this flag */
692         OPT_L = 0x2, // -L /* Stub warning */
693                 /* Compatibility with modprobe.
694                    In theory, this does locking, but we don't do
695                    that.  So be careful and plan your life around not
696                    loading the same module 50 times concurrently. */
697         OPT_o = 0x4, // -o /* name the output module */
698         OPT_f = 0x8, // -f /* force loading */
699         OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
700         OPT_v = 0x20, // -v /* verbose output */
701         OPT_q = 0x40, // -q /* silent */
702         OPT_x = 0x80, // -x /* do not export externs */
703         OPT_m = 0x100, // -m /* print module load map */
704 };
705 #define flag_force_load (option_mask32 & OPT_f)
706 #define flag_autoclean (option_mask32 & OPT_k)
707 #define flag_verbose (option_mask32 & OPT_v)
708 #define flag_quiet (option_mask32 & OPT_q)
709 #define flag_noexport (option_mask32 & OPT_x)
710 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
711 #define flag_print_load_map (option_mask32 & OPT_m)
712 #else
713 #define flag_print_load_map 0
714 #endif
715
716 /*======================================================================*/
717
718 #if defined(USE_LIST)
719
720 struct arch_list_entry
721 {
722         struct arch_list_entry *next;
723         LIST_ARCHTYPE addend;
724         int offset;
725         int inited : 1;
726 };
727
728 #endif
729
730 #if defined(USE_SINGLE)
731
732 struct arch_single_entry
733 {
734         int offset;
735         int inited : 1;
736         int allocated : 1;
737 };
738
739 #endif
740
741 #if defined(__mips__)
742 struct mips_hi16
743 {
744         struct mips_hi16 *next;
745         ElfW(Addr) *addr;
746         ElfW(Addr) value;
747 };
748 #endif
749
750 struct arch_file {
751         struct obj_file root;
752 #if defined(USE_PLT_ENTRIES)
753         struct obj_section *plt;
754 #endif
755 #if defined(USE_GOT_ENTRIES)
756         struct obj_section *got;
757 #endif
758 #if defined(__mips__)
759         struct mips_hi16 *mips_hi16_list;
760 #endif
761 };
762
763 struct arch_symbol {
764         struct obj_symbol root;
765 #if defined(USE_PLT_ENTRIES)
766 #if defined(USE_PLT_LIST)
767         struct arch_list_entry *pltent;
768 #else
769         struct arch_single_entry pltent;
770 #endif
771 #endif
772 #if defined(USE_GOT_ENTRIES)
773         struct arch_single_entry gotent;
774 #endif
775 };
776
777
778 struct external_module {
779         const char *name;
780         ElfW(Addr) addr;
781         int used;
782         size_t nsyms;
783         struct new_module_symbol *syms;
784 };
785
786 static struct new_module_symbol *ksyms;
787 static size_t nksyms;
788
789 static struct external_module *ext_modules;
790 static int n_ext_modules;
791 static int n_ext_modules_used;
792 extern int delete_module(const char *);
793
794 static char *m_filename;
795 static char *m_fullName;
796
797
798 /*======================================================================*/
799
800
801 static int check_module_name_match(const char *filename, struct stat *statbuf,
802                                 void *userdata, int depth)
803 {
804         char *fullname = (char *) userdata;
805
806         if (fullname[0] == '\0')
807                 return FALSE;
808         else {
809                 char *tmp, *tmp1 = xstrdup(filename);
810                 tmp = bb_get_last_path_component(tmp1);
811                 if (strcmp(tmp, fullname) == 0) {
812                         free(tmp1);
813                         /* Stop searching if we find a match */
814                         m_filename = xstrdup(filename);
815                         return FALSE;
816                 }
817                 free(tmp1);
818         }
819         return TRUE;
820 }
821
822
823 /*======================================================================*/
824
825 static struct obj_file *arch_new_file(void)
826 {
827         struct arch_file *f;
828         f = xmalloc(sizeof(*f));
829
830         memset(f, 0, sizeof(*f));
831
832         return &f->root;
833 }
834
835 static struct obj_section *arch_new_section(void)
836 {
837         return xmalloc(sizeof(struct obj_section));
838 }
839
840 static struct obj_symbol *arch_new_symbol(void)
841 {
842         struct arch_symbol *sym;
843         sym = xmalloc(sizeof(*sym));
844
845         memset(sym, 0, sizeof(*sym));
846
847         return &sym->root;
848 }
849
850 static enum obj_reloc
851 arch_apply_relocation(struct obj_file *f,
852                                 struct obj_section *targsec,
853                                 struct obj_section *symsec,
854                                 struct obj_symbol *sym,
855                                 ElfW(RelM) *rel, ElfW(Addr) v)
856 {
857         struct arch_file *ifile = (struct arch_file *) f;
858         enum obj_reloc ret = obj_reloc_ok;
859         ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
860         ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
861 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
862         struct arch_symbol *isym = (struct arch_symbol *) sym;
863 #endif
864 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
865 #if defined(USE_GOT_ENTRIES)
866         ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
867 #endif
868 #endif
869 #if defined(USE_PLT_ENTRIES)
870         ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
871         unsigned long *ip;
872 # if defined(USE_PLT_LIST)
873         struct arch_list_entry *pe;
874 # else
875         struct arch_single_entry *pe;
876 # endif
877 #endif
878
879         switch (ELF_R_TYPE(rel->r_info)) {
880
881 #if defined(__arm__)
882
883                 case R_ARM_NONE:
884                         break;
885
886                 case R_ARM_ABS32:
887                         *loc += v;
888                         break;
889
890                 case R_ARM_GOT32:
891                         goto bb_use_got;
892
893                 case R_ARM_GOTPC:
894                         /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
895                          * (which is .got) similar to branch,
896                          * but is full 32 bits relative */
897
898                         *loc += got - dot;
899                         break;
900
901                 case R_ARM_PC24:
902                 case R_ARM_PLT32:
903                         goto bb_use_plt;
904
905                 case R_ARM_GOTOFF: /* address relative to the got */
906                         *loc += v - got;
907                         break;
908
909 #elif defined(__cris__)
910
911                 case R_CRIS_NONE:
912                         break;
913
914                 case R_CRIS_32:
915                         /* CRIS keeps the relocation value in the r_addend field and
916                          * should not use whats in *loc at all
917                          */
918                         *loc = v;
919                         break;
920
921 #elif defined(__H8300H__) || defined(__H8300S__)
922
923                 case R_H8_DIR24R8:
924                         loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
925                         *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
926                         break;
927                 case R_H8_DIR24A8:
928                         *loc += v;
929                         break;
930                 case R_H8_DIR32:
931                 case R_H8_DIR32A16:
932                         *loc += v;
933                         break;
934                 case R_H8_PCREL16:
935                         v -= dot + 2;
936                         if ((ElfW(Sword))v > 0x7fff ||
937                             (ElfW(Sword))v < -(ElfW(Sword))0x8000)
938                                 ret = obj_reloc_overflow;
939                         else
940                                 *(unsigned short *)loc = v;
941                         break;
942                 case R_H8_PCREL8:
943                         v -= dot + 1;
944                         if ((ElfW(Sword))v > 0x7f ||
945                             (ElfW(Sword))v < -(ElfW(Sword))0x80)
946                                 ret = obj_reloc_overflow;
947                         else
948                                 *(unsigned char *)loc = v;
949                         break;
950
951 #elif defined(__i386__)
952
953                 case R_386_NONE:
954                         break;
955
956                 case R_386_32:
957                         *loc += v;
958                         break;
959
960                 case R_386_PLT32:
961                 case R_386_PC32:
962                         *loc += v - dot;
963                         break;
964
965                 case R_386_GLOB_DAT:
966                 case R_386_JMP_SLOT:
967                         *loc = v;
968                         break;
969
970                 case R_386_RELATIVE:
971                         *loc += f->baseaddr;
972                         break;
973
974                 case R_386_GOTPC:
975                         *loc += got - dot;
976                         break;
977
978                 case R_386_GOT32:
979                         goto bb_use_got;
980
981                 case R_386_GOTOFF:
982                         *loc += v - got;
983                         break;
984
985 #elif defined (__microblaze__)
986                 case R_MICROBLAZE_NONE:
987                 case R_MICROBLAZE_64_NONE:
988                 case R_MICROBLAZE_32_SYM_OP_SYM:
989                 case R_MICROBLAZE_32_PCREL:
990                         break;
991
992                 case R_MICROBLAZE_64_PCREL: {
993                         /* dot is the address of the current instruction.
994                          * v is the target symbol address.
995                          * So we need to extract the offset in the code,
996                          * adding v, then subtrating the current address
997                          * of this instruction.
998                          * Ex: "IMM 0xFFFE  bralid 0x0000" = "bralid 0xFFFE0000"
999                          */
1000
1001                         /* Get split offset stored in code */
1002                         unsigned int temp = (loc[0] & 0xFFFF) << 16 |
1003                                                 (loc[1] & 0xFFFF);
1004
1005                         /* Adjust relative offset. -4 adjustment required
1006                          * because dot points to the IMM insn, but branch
1007                          * is computed relative to the branch instruction itself.
1008                          */
1009                         temp += v - dot - 4;
1010
1011                         /* Store back into code */
1012                         loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1013                         loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1014
1015                         break;
1016                 }
1017
1018                 case R_MICROBLAZE_32:
1019                         *loc += v;
1020                         break;
1021
1022                 case R_MICROBLAZE_64: {
1023                         /* Get split pointer stored in code */
1024                         unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1025                                                 (loc[1] & 0xFFFF);
1026
1027                         /* Add reloc offset */
1028                         temp1+=v;
1029
1030                         /* Store back into code */
1031                         loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1032                         loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1033
1034                         break;
1035                 }
1036
1037                 case R_MICROBLAZE_32_PCREL_LO:
1038                 case R_MICROBLAZE_32_LO:
1039                 case R_MICROBLAZE_SRO32:
1040                 case R_MICROBLAZE_SRW32:
1041                         ret = obj_reloc_unhandled;
1042                         break;
1043
1044 #elif defined(__mc68000__)
1045
1046                 case R_68K_NONE:
1047                         break;
1048
1049                 case R_68K_32:
1050                         *loc += v;
1051                         break;
1052
1053                 case R_68K_8:
1054                         if (v > 0xff) {
1055                                 ret = obj_reloc_overflow;
1056                         }
1057                         *(char *)loc = v;
1058                         break;
1059
1060                 case R_68K_16:
1061                         if (v > 0xffff) {
1062                                 ret = obj_reloc_overflow;
1063                         }
1064                         *(short *)loc = v;
1065                         break;
1066
1067                 case R_68K_PC8:
1068                         v -= dot;
1069                         if ((ElfW(Sword))v > 0x7f ||
1070                                         (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1071                                 ret = obj_reloc_overflow;
1072                         }
1073                         *(char *)loc = v;
1074                         break;
1075
1076                 case R_68K_PC16:
1077                         v -= dot;
1078                         if ((ElfW(Sword))v > 0x7fff ||
1079                                         (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1080                                 ret = obj_reloc_overflow;
1081                         }
1082                         *(short *)loc = v;
1083                         break;
1084
1085                 case R_68K_PC32:
1086                         *(int *)loc = v - dot;
1087                         break;
1088
1089                 case R_68K_GLOB_DAT:
1090                 case R_68K_JMP_SLOT:
1091                         *loc = v;
1092                         break;
1093
1094                 case R_68K_RELATIVE:
1095                         *(int *)loc += f->baseaddr;
1096                         break;
1097
1098                 case R_68K_GOT32:
1099                         goto bb_use_got;
1100
1101 # ifdef R_68K_GOTOFF
1102                 case R_68K_GOTOFF:
1103                         *loc += v - got;
1104                         break;
1105 # endif
1106
1107 #elif defined(__mips__)
1108
1109                 case R_MIPS_NONE:
1110                         break;
1111
1112                 case R_MIPS_32:
1113                         *loc += v;
1114                         break;
1115
1116                 case R_MIPS_26:
1117                         if (v % 4)
1118                                 ret = obj_reloc_dangerous;
1119                         if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1120                                 ret = obj_reloc_overflow;
1121                         *loc =
1122                                 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1123                                                                                 0x03ffffff);
1124                         break;
1125
1126                 case R_MIPS_HI16:
1127                         {
1128                                 struct mips_hi16 *n;
1129
1130                                 /* We cannot relocate this one now because we don't know the value
1131                                    of the carry we need to add.  Save the information, and let LO16
1132                                    do the actual relocation.  */
1133                                 n = xmalloc(sizeof *n);
1134                                 n->addr = loc;
1135                                 n->value = v;
1136                                 n->next = ifile->mips_hi16_list;
1137                                 ifile->mips_hi16_list = n;
1138                                 break;
1139                         }
1140
1141                 case R_MIPS_LO16:
1142                         {
1143                                 unsigned long insnlo = *loc;
1144                                 ElfW(Addr) val, vallo;
1145
1146                                 /* Sign extend the addend we extract from the lo insn.  */
1147                                 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1148
1149                                 if (ifile->mips_hi16_list != NULL) {
1150                                         struct mips_hi16 *l;
1151
1152                                         l = ifile->mips_hi16_list;
1153                                         while (l != NULL) {
1154                                                 struct mips_hi16 *next;
1155                                                 unsigned long insn;
1156
1157                                                 /* Do the HI16 relocation.  Note that we actually don't
1158                                                    need to know anything about the LO16 itself, except where
1159                                                    to find the low 16 bits of the addend needed by the LO16.  */
1160                                                 insn = *l->addr;
1161                                                 val =
1162                                                         ((insn & 0xffff) << 16) +
1163                                                         vallo;
1164                                                 val += v;
1165
1166                                                 /* Account for the sign extension that will happen in the
1167                                                    low bits.  */
1168                                                 val =
1169                                                         ((val >> 16) +
1170                                                          ((val & 0x8000) !=
1171                                                           0)) & 0xffff;
1172
1173                                                 insn = (insn & ~0xffff) | val;
1174                                                 *l->addr = insn;
1175
1176                                                 next = l->next;
1177                                                 free(l);
1178                                                 l = next;
1179                                         }
1180
1181                                         ifile->mips_hi16_list = NULL;
1182                                 }
1183
1184                                 /* Ok, we're done with the HI16 relocs.  Now deal with the LO16.  */
1185                                 val = v + vallo;
1186                                 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1187                                 *loc = insnlo;
1188                                 break;
1189                         }
1190
1191 #elif defined(__nios2__)
1192
1193                 case R_NIOS2_NONE:
1194                         break;
1195
1196                 case R_NIOS2_BFD_RELOC_32:
1197                         *loc += v;
1198                         break;
1199
1200                 case R_NIOS2_BFD_RELOC_16:
1201                         if (v > 0xffff) {
1202                                 ret = obj_reloc_overflow;
1203                         }
1204                         *(short *)loc = v;
1205                         break;
1206
1207                 case R_NIOS2_BFD_RELOC_8:
1208                         if (v > 0xff) {
1209                                 ret = obj_reloc_overflow;
1210                         }
1211                         *(char *)loc = v;
1212                         break;
1213
1214                 case R_NIOS2_S16:
1215                         {
1216                                 Elf32_Addr word;
1217
1218                                 if ((Elf32_Sword)v > 0x7fff ||
1219                                     (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1220                                         ret = obj_reloc_overflow;
1221                                 }
1222
1223                                 word = *loc;
1224                                 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1225                                        (word & 0x3f);
1226                         }
1227                         break;
1228
1229                 case R_NIOS2_U16:
1230                         {
1231                                 Elf32_Addr word;
1232
1233                                 if (v > 0xffff) {
1234                                         ret = obj_reloc_overflow;
1235                                 }
1236
1237                                 word = *loc;
1238                                 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1239                                        (word & 0x3f);
1240                         }
1241                         break;
1242
1243                 case R_NIOS2_PCREL16:
1244                         {
1245                                 Elf32_Addr word;
1246
1247                                 v -= dot + 4;
1248                                 if ((Elf32_Sword)v > 0x7fff ||
1249                                     (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1250                                         ret = obj_reloc_overflow;
1251                                 }
1252
1253                                 word = *loc;
1254                                 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1255                         }
1256                         break;
1257
1258                 case R_NIOS2_GPREL:
1259                         {
1260                                 Elf32_Addr word, gp;
1261                                 /* get _gp */
1262                                 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1263                                 v-=gp;
1264                                 if ((Elf32_Sword)v > 0x7fff ||
1265                                                 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1266                                         ret = obj_reloc_overflow;
1267                                 }
1268
1269                                 word = *loc;
1270                                 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1271                         }
1272                         break;
1273
1274                 case R_NIOS2_CALL26:
1275                         if (v & 3)
1276                                 ret = obj_reloc_dangerous;
1277                         if ((v >> 28) != (dot >> 28))
1278                                 ret = obj_reloc_overflow;
1279                         *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1280                         break;
1281
1282                 case R_NIOS2_IMM5:
1283                         {
1284                                 Elf32_Addr word;
1285
1286                                 if (v > 0x1f) {
1287                                         ret = obj_reloc_overflow;
1288                                 }
1289
1290                                 word = *loc & ~0x7c0;
1291                                 *loc = word | ((v & 0x1f) << 6);
1292                         }
1293                         break;
1294
1295                 case R_NIOS2_IMM6:
1296                         {
1297                                 Elf32_Addr word;
1298
1299                                 if (v > 0x3f) {
1300                                         ret = obj_reloc_overflow;
1301                                 }
1302
1303                                 word = *loc & ~0xfc0;
1304                                 *loc = word | ((v & 0x3f) << 6);
1305                         }
1306                         break;
1307
1308                 case R_NIOS2_IMM8:
1309                         {
1310                                 Elf32_Addr word;
1311
1312                                 if (v > 0xff) {
1313                                         ret = obj_reloc_overflow;
1314                                 }
1315
1316                                 word = *loc & ~0x3fc0;
1317                                 *loc = word | ((v & 0xff) << 6);
1318                         }
1319                         break;
1320
1321                 case R_NIOS2_HI16:
1322                         {
1323                                 Elf32_Addr word;
1324
1325                                 word = *loc;
1326                                 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1327                                        (word & 0x3f);
1328                         }
1329                         break;
1330
1331                 case R_NIOS2_LO16:
1332                         {
1333                                 Elf32_Addr word;
1334
1335                                 word = *loc;
1336                                 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1337                                        (word & 0x3f);
1338                         }
1339                         break;
1340
1341                 case R_NIOS2_HIADJ16:
1342                         {
1343                                 Elf32_Addr word1, word2;
1344
1345                                 word1 = *loc;
1346                                 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1347                                 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1348                                        (word1 & 0x3f);
1349                         }
1350                         break;
1351
1352 #elif defined(__powerpc64__)
1353                 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1354
1355 #elif defined(__powerpc__)
1356
1357                 case R_PPC_ADDR16_HA:
1358                         *(unsigned short *)loc = (v + 0x8000) >> 16;
1359                         break;
1360
1361                 case R_PPC_ADDR16_HI:
1362                         *(unsigned short *)loc = v >> 16;
1363                         break;
1364
1365                 case R_PPC_ADDR16_LO:
1366                         *(unsigned short *)loc = v;
1367                         break;
1368
1369                 case R_PPC_REL24:
1370                         goto bb_use_plt;
1371
1372                 case R_PPC_REL32:
1373                         *loc = v - dot;
1374                         break;
1375
1376                 case R_PPC_ADDR32:
1377                         *loc = v;
1378                         break;
1379
1380 #elif defined(__s390__)
1381
1382                 case R_390_32:
1383                         *(unsigned int *) loc += v;
1384                         break;
1385                 case R_390_16:
1386                         *(unsigned short *) loc += v;
1387                         break;
1388                 case R_390_8:
1389                         *(unsigned char *) loc += v;
1390                         break;
1391
1392                 case R_390_PC32:
1393                         *(unsigned int *) loc += v - dot;
1394                         break;
1395                 case R_390_PC16DBL:
1396                         *(unsigned short *) loc += (v - dot) >> 1;
1397                         break;
1398                 case R_390_PC16:
1399                         *(unsigned short *) loc += v - dot;
1400                         break;
1401
1402                 case R_390_PLT32:
1403                 case R_390_PLT16DBL:
1404                         /* find the plt entry and initialize it.  */
1405                         pe = (struct arch_single_entry *) &isym->pltent;
1406                         if (pe->inited == 0) {
1407                                 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1408                                 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1409                                 ip[1] = 0x100607f1;
1410                                 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1411                                         ip[2] = v - 2;
1412                                 else
1413                                         ip[2] = v;
1414                                 pe->inited = 1;
1415                         }
1416
1417                         /* Insert relative distance to target.  */
1418                         v = plt + pe->offset - dot;
1419                         if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1420                                 *(unsigned int *) loc = (unsigned int) v;
1421                         else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1422                                 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1423                         break;
1424
1425                 case R_390_GLOB_DAT:
1426                 case R_390_JMP_SLOT:
1427                         *loc = v;
1428                         break;
1429
1430                 case R_390_RELATIVE:
1431                         *loc += f->baseaddr;
1432                         break;
1433
1434                 case R_390_GOTPC:
1435                         *(unsigned long *) loc += got - dot;
1436                         break;
1437
1438                 case R_390_GOT12:
1439                 case R_390_GOT16:
1440                 case R_390_GOT32:
1441                         if (!isym->gotent.inited)
1442                         {
1443                                 isym->gotent.inited = 1;
1444                                 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1445                         }
1446                         if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1447                                 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1448                         else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1449                                 *(unsigned short *) loc += isym->gotent.offset;
1450                         else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1451                                 *(unsigned int *) loc += isym->gotent.offset;
1452                         break;
1453
1454 # ifndef R_390_GOTOFF32
1455 #  define R_390_GOTOFF32 R_390_GOTOFF
1456 # endif
1457                 case R_390_GOTOFF32:
1458                         *loc += v - got;
1459                         break;
1460
1461 #elif defined(__sh__)
1462
1463                 case R_SH_NONE:
1464                         break;
1465
1466                 case R_SH_DIR32:
1467                         *loc += v;
1468                         break;
1469
1470                 case R_SH_REL32:
1471                         *loc += v - dot;
1472                         break;
1473
1474                 case R_SH_PLT32:
1475                         *loc = v - dot;
1476                         break;
1477
1478                 case R_SH_GLOB_DAT:
1479                 case R_SH_JMP_SLOT:
1480                         *loc = v;
1481                         break;
1482
1483                 case R_SH_RELATIVE:
1484                         *loc = f->baseaddr + rel->r_addend;
1485                         break;
1486
1487                 case R_SH_GOTPC:
1488                         *loc = got - dot + rel->r_addend;
1489                         break;
1490
1491                 case R_SH_GOT32:
1492                         goto bb_use_got;
1493
1494                 case R_SH_GOTOFF:
1495                         *loc = v - got;
1496                         break;
1497
1498 # if defined(__SH5__)
1499                 case R_SH_IMM_MEDLOW16:
1500                 case R_SH_IMM_LOW16:
1501                         {
1502                                 ElfW(Addr) word;
1503
1504                                 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1505                                         v >>= 16;
1506
1507                                 /*
1508                                  *  movi and shori have the format:
1509                                  *
1510                                  *  |  op  | imm  | reg | reserved |
1511                                  *   31..26 25..10 9.. 4 3   ..   0
1512                                  *
1513                                  * so we simply mask and or in imm.
1514                                  */
1515                                 word = *loc & ~0x3fffc00;
1516                                 word |= (v & 0xffff) << 10;
1517
1518                                 *loc = word;
1519
1520                                 break;
1521                         }
1522
1523                 case R_SH_IMM_MEDLOW16_PCREL:
1524                 case R_SH_IMM_LOW16_PCREL:
1525                         {
1526                                 ElfW(Addr) word;
1527
1528                                 word = *loc & ~0x3fffc00;
1529
1530                                 v -= dot;
1531
1532                                 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1533                                         v >>= 16;
1534
1535                                 word |= (v & 0xffff) << 10;
1536
1537                                 *loc = word;
1538
1539                                 break;
1540                         }
1541 # endif /* __SH5__ */
1542
1543 #elif defined (__v850e__)
1544
1545                 case R_V850_NONE:
1546                         break;
1547
1548                 case R_V850_32:
1549                         /* We write two shorts instead of a long because even
1550                            32-bit insns only need half-word alignment, but
1551                            32-bit data needs to be long-word aligned.  */
1552                         v += ((unsigned short *)loc)[0];
1553                         v += ((unsigned short *)loc)[1] << 16;
1554                         ((unsigned short *)loc)[0] = v & 0xffff;
1555                         ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1556                         break;
1557
1558                 case R_V850_22_PCREL:
1559                         goto bb_use_plt;
1560
1561 #elif defined(__x86_64__)
1562
1563                 case R_X86_64_NONE:
1564                         break;
1565
1566                 case R_X86_64_64:
1567                         *loc += v;
1568                         break;
1569
1570                 case R_X86_64_32:
1571                         *(unsigned int *) loc += v;
1572                         if (v > 0xffffffff)
1573                         {
1574                                 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1575                                 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1576                         }
1577                         break;
1578
1579                 case R_X86_64_32S:
1580                         *(signed int *) loc += v;
1581                         break;
1582
1583                 case R_X86_64_16:
1584                         *(unsigned short *) loc += v;
1585                         break;
1586
1587                 case R_X86_64_8:
1588                         *(unsigned char *) loc += v;
1589                         break;
1590
1591                 case R_X86_64_PC32:
1592                         *(unsigned int *) loc += v - dot;
1593                         break;
1594
1595                 case R_X86_64_PC16:
1596                         *(unsigned short *) loc += v - dot;
1597                         break;
1598
1599                 case R_X86_64_PC8:
1600                         *(unsigned char *) loc += v - dot;
1601                         break;
1602
1603                 case R_X86_64_GLOB_DAT:
1604                 case R_X86_64_JUMP_SLOT:
1605                         *loc = v;
1606                         break;
1607
1608                 case R_X86_64_RELATIVE:
1609                         *loc += f->baseaddr;
1610                         break;
1611
1612                 case R_X86_64_GOT32:
1613                 case R_X86_64_GOTPCREL:
1614                         goto bb_use_got;
1615 # if 0
1616                         if (!isym->gotent.reloc_done)
1617                         {
1618                                 isym->gotent.reloc_done = 1;
1619                                 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1620                         }
1621                         /* XXX are these really correct?  */
1622                         if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1623                                 *(unsigned int *) loc += v + isym->gotent.offset;
1624                         else
1625                                 *loc += isym->gotent.offset;
1626                         break;
1627 # endif
1628
1629 #else
1630 # warning "no idea how to handle relocations on your arch"
1631 #endif
1632
1633                 default:
1634                         printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1635                         ret = obj_reloc_unhandled;
1636                         break;
1637
1638 #if defined(USE_PLT_ENTRIES)
1639
1640 bb_use_plt:
1641
1642                         /* find the plt entry and initialize it if necessary */
1643
1644 #if defined(USE_PLT_LIST)
1645                         for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1646                                 pe = pe->next;
1647 #else
1648                         pe = &isym->pltent;
1649 #endif
1650
1651                         if (! pe->inited) {
1652                                 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1653
1654                                 /* generate some machine code */
1655
1656 #if defined(__arm__)
1657                                 ip[0] = 0xe51ff004;                     /* ldr pc,[pc,#-4] */
1658                                 ip[1] = v;                              /* sym@ */
1659 #endif
1660 #if defined(__powerpc__)
1661                                 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16);  /* lis r11,sym@ha */
1662                                 ip[1] = 0x396b0000 + (v & 0xffff);          /* addi r11,r11,sym@l */
1663                                 ip[2] = 0x7d6903a6;                           /* mtctr r11 */
1664                                 ip[3] = 0x4e800420;                           /* bctr */
1665 #endif
1666 #if defined (__v850e__)
1667                                 /* We have to trash a register, so we assume that any control
1668                                    transfer more than 21-bits away must be a function call
1669                                    (so we can use a call-clobbered register).  */
1670                                 ip[0] = 0x0621 + ((v & 0xffff) << 16);   /* mov sym, r1 ... */
1671                                 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1672 #endif
1673                                 pe->inited = 1;
1674                         }
1675
1676                         /* relative distance to target */
1677                         v -= dot;
1678                         /* if the target is too far away.... */
1679 #if defined (__arm__) || defined (__powerpc__)
1680                         if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1681 #elif defined (__v850e__)
1682                                 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1683 #endif
1684                                         /* go via the plt */
1685                                         v = plt + pe->offset - dot;
1686
1687 #if defined (__v850e__)
1688                         if (v & 1)
1689 #else
1690                                 if (v & 3)
1691 #endif
1692                                         ret = obj_reloc_dangerous;
1693
1694                         /* merge the offset into the instruction. */
1695 #if defined(__arm__)
1696                         /* Convert to words. */
1697                         v >>= 2;
1698
1699                         *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1700 #endif
1701 #if defined(__powerpc__)
1702                         *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1703 #endif
1704 #if defined (__v850e__)
1705                         /* We write two shorts instead of a long because even 32-bit insns
1706                            only need half-word alignment, but the 32-bit data write needs
1707                            to be long-word aligned.  */
1708                         ((unsigned short *)loc)[0] =
1709                                 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1710                                 | ((v >> 16) & 0x3f);             /* offs high part */
1711                         ((unsigned short *)loc)[1] =
1712                                 (v & 0xffff);                    /* offs low part */
1713 #endif
1714                         break;
1715 #endif /* USE_PLT_ENTRIES */
1716
1717 #if defined(USE_GOT_ENTRIES)
1718 bb_use_got:
1719
1720                         /* needs an entry in the .got: set it, once */
1721                         if (!isym->gotent.inited) {
1722                                 isym->gotent.inited = 1;
1723                                 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1724                         }
1725                         /* make the reloc with_respect_to_.got */
1726 #if defined(__sh__)
1727                         *loc += isym->gotent.offset + rel->r_addend;
1728 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1729                         *loc += isym->gotent.offset;
1730 #endif
1731                         break;
1732
1733 #endif /* USE_GOT_ENTRIES */
1734         }
1735
1736         return ret;
1737 }
1738
1739
1740 #if defined(USE_LIST)
1741
1742 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1743                           int offset, int size)
1744 {
1745         struct arch_list_entry *pe;
1746
1747         for (pe = *list; pe != NULL; pe = pe->next) {
1748                 if (pe->addend == rel->r_addend) {
1749                         break;
1750                 }
1751         }
1752
1753         if (pe == NULL) {
1754                 pe = xmalloc(sizeof(struct arch_list_entry));
1755                 pe->next = *list;
1756                 pe->addend = rel->r_addend;
1757                 pe->offset = offset;
1758                 pe->inited = 0;
1759                 *list = pe;
1760                 return size;
1761         }
1762         return 0;
1763 }
1764
1765 #endif
1766
1767 #if defined(USE_SINGLE)
1768
1769 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1770                              int offset, int size)
1771 {
1772         if (single->allocated == 0) {
1773                 single->allocated = 1;
1774                 single->offset = offset;
1775                 single->inited = 0;
1776                 return size;
1777         }
1778         return 0;
1779 }
1780
1781 #endif
1782
1783 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1784
1785 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1786                                            int offset, int size)
1787 {
1788         struct obj_section *myrelsec = obj_find_section(f, name);
1789
1790         if (offset == 0) {
1791                 offset += size;
1792         }
1793
1794         if (myrelsec) {
1795                 obj_extend_section(myrelsec, offset);
1796         } else {
1797                 myrelsec = obj_create_alloced_section(f, name,
1798                                 size, offset);
1799         }
1800
1801         return myrelsec;
1802 }
1803
1804 #endif
1805
1806 static void arch_create_got(struct obj_file *f)
1807 {
1808 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1809         struct arch_file *ifile = (struct arch_file *) f;
1810         int i;
1811 #if defined(USE_GOT_ENTRIES)
1812         int got_offset = 0, got_needed = 0, got_allocate;
1813 #endif
1814 #if defined(USE_PLT_ENTRIES)
1815         int plt_offset = 0, plt_needed = 0, plt_allocate;
1816 #endif
1817         struct obj_section *relsec, *symsec, *strsec;
1818         ElfW(RelM) *rel, *relend;
1819         ElfW(Sym) *symtab, *extsym;
1820         const char *strtab, *name;
1821         struct arch_symbol *intsym;
1822
1823         for (i = 0; i < f->header.e_shnum; ++i) {
1824                 relsec = f->sections[i];
1825                 if (relsec->header.sh_type != SHT_RELM)
1826                         continue;
1827
1828                 symsec = f->sections[relsec->header.sh_link];
1829                 strsec = f->sections[symsec->header.sh_link];
1830
1831                 rel = (ElfW(RelM) *) relsec->contents;
1832                 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1833                 symtab = (ElfW(Sym) *) symsec->contents;
1834                 strtab = (const char *) strsec->contents;
1835
1836                 for (; rel < relend; ++rel) {
1837                         extsym = &symtab[ELF_R_SYM(rel->r_info)];
1838
1839 #if defined(USE_GOT_ENTRIES)
1840                         got_allocate = 0;
1841 #endif
1842 #if defined(USE_PLT_ENTRIES)
1843                         plt_allocate = 0;
1844 #endif
1845
1846                         switch (ELF_R_TYPE(rel->r_info)) {
1847 #if defined(__arm__)
1848                         case R_ARM_PC24:
1849                         case R_ARM_PLT32:
1850                                 plt_allocate = 1;
1851                                 break;
1852
1853                         case R_ARM_GOTOFF:
1854                         case R_ARM_GOTPC:
1855                                 got_needed = 1;
1856                                 continue;
1857
1858                         case R_ARM_GOT32:
1859                                 got_allocate = 1;
1860                                 break;
1861
1862 #elif defined(__i386__)
1863                         case R_386_GOTPC:
1864                         case R_386_GOTOFF:
1865                                 got_needed = 1;
1866                                 continue;
1867
1868                         case R_386_GOT32:
1869                                 got_allocate = 1;
1870                                 break;
1871
1872 #elif defined(__powerpc__)
1873                         case R_PPC_REL24:
1874                                 plt_allocate = 1;
1875                                 break;
1876
1877 #elif defined(__mc68000__)
1878                         case R_68K_GOT32:
1879                                 got_allocate = 1;
1880                                 break;
1881
1882 #ifdef R_68K_GOTOFF
1883                         case R_68K_GOTOFF:
1884                                 got_needed = 1;
1885                                 continue;
1886 #endif
1887
1888 #elif defined(__sh__)
1889                         case R_SH_GOT32:
1890                                 got_allocate = 1;
1891                                 break;
1892
1893                         case R_SH_GOTPC:
1894                         case R_SH_GOTOFF:
1895                                 got_needed = 1;
1896                                 continue;
1897
1898 #elif defined (__v850e__)
1899                         case R_V850_22_PCREL:
1900                                 plt_needed = 1;
1901                                 break;
1902
1903 #endif
1904                         default:
1905                                 continue;
1906                         }
1907
1908                         if (extsym->st_name != 0) {
1909                                 name = strtab + extsym->st_name;
1910                         } else {
1911                                 name = f->sections[extsym->st_shndx]->name;
1912                         }
1913                         intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1914 #if defined(USE_GOT_ENTRIES)
1915                         if (got_allocate) {
1916                                 got_offset += arch_single_init(
1917                                                 rel, &intsym->gotent,
1918                                                 got_offset, GOT_ENTRY_SIZE);
1919
1920                                 got_needed = 1;
1921                         }
1922 #endif
1923 #if defined(USE_PLT_ENTRIES)
1924                         if (plt_allocate) {
1925 #if defined(USE_PLT_LIST)
1926                                 plt_offset += arch_list_add(
1927                                                 rel, &intsym->pltent,
1928                                                 plt_offset, PLT_ENTRY_SIZE);
1929 #else
1930                                 plt_offset += arch_single_init(
1931                                                 rel, &intsym->pltent,
1932                                                 plt_offset, PLT_ENTRY_SIZE);
1933 #endif
1934                                 plt_needed = 1;
1935                         }
1936 #endif
1937                 }
1938         }
1939
1940 #if defined(USE_GOT_ENTRIES)
1941         if (got_needed) {
1942                 ifile->got = arch_xsect_init(f, ".got", got_offset,
1943                                 GOT_ENTRY_SIZE);
1944         }
1945 #endif
1946
1947 #if defined(USE_PLT_ENTRIES)
1948         if (plt_needed) {
1949                 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1950                                 PLT_ENTRY_SIZE);
1951         }
1952 #endif
1953
1954 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1955 }
1956
1957 /*======================================================================*/
1958
1959 /* Standard ELF hash function.  */
1960 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1961 {
1962         unsigned long h = 0;
1963         unsigned long g;
1964         unsigned char ch;
1965
1966         while (n > 0) {
1967                 ch = *name++;
1968                 h = (h << 4) + ch;
1969                 if ((g = (h & 0xf0000000)) != 0) {
1970                         h ^= g >> 24;
1971                         h &= ~g;
1972                 }
1973                 n--;
1974         }
1975         return h;
1976 }
1977
1978 static unsigned long obj_elf_hash(const char *name)
1979 {
1980         return obj_elf_hash_n(name, strlen(name));
1981 }
1982
1983 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1984 /* String comparison for non-co-versioned kernel and module.  */
1985
1986 static int ncv_strcmp(const char *a, const char *b)
1987 {
1988         size_t alen = strlen(a), blen = strlen(b);
1989
1990         if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1991                 return strncmp(a, b, alen);
1992         else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1993                 return strncmp(a, b, blen);
1994         else
1995                 return strcmp(a, b);
1996 }
1997
1998 /* String hashing for non-co-versioned kernel and module.  Here
1999    we are simply forced to drop the crc from the hash.  */
2000
2001 static unsigned long ncv_symbol_hash(const char *str)
2002 {
2003         size_t len = strlen(str);
2004         if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2005                 len -= 10;
2006         return obj_elf_hash_n(str, len);
2007 }
2008
2009 static void
2010 obj_set_symbol_compare(struct obj_file *f,
2011                                            int (*cmp) (const char *, const char *),
2012                                            unsigned long (*hash) (const char *))
2013 {
2014         if (cmp)
2015                 f->symbol_cmp = cmp;
2016         if (hash) {
2017                 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2018                 int i;
2019
2020                 f->symbol_hash = hash;
2021
2022                 memcpy(tmptab, f->symtab, sizeof(tmptab));
2023                 memset(f->symtab, 0, sizeof(f->symtab));
2024
2025                 for (i = 0; i < HASH_BUCKETS; ++i)
2026                         for (sym = tmptab[i]; sym; sym = next) {
2027                                 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2028                                 next = sym->next;
2029                                 sym->next = f->symtab[h];
2030                                 f->symtab[h] = sym;
2031                         }
2032         }
2033 }
2034
2035 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2036
2037 static struct obj_symbol *
2038 obj_add_symbol(struct obj_file *f, const char *name,
2039                                 unsigned long symidx, int info,
2040                                 int secidx, ElfW(Addr) value,
2041                                 unsigned long size)
2042 {
2043         struct obj_symbol *sym;
2044         unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2045         int n_type = ELF_ST_TYPE(info);
2046         int n_binding = ELF_ST_BIND(info);
2047
2048         for (sym = f->symtab[hash]; sym; sym = sym->next)
2049                 if (f->symbol_cmp(sym->name, name) == 0) {
2050                         int o_secidx = sym->secidx;
2051                         int o_info = sym->info;
2052                         int o_type = ELF_ST_TYPE(o_info);
2053                         int o_binding = ELF_ST_BIND(o_info);
2054
2055                         /* A redefinition!  Is it legal?  */
2056
2057                         if (secidx == SHN_UNDEF)
2058                                 return sym;
2059                         else if (o_secidx == SHN_UNDEF)
2060                                 goto found;
2061                         else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2062                                 /* Cope with local and global symbols of the same name
2063                                    in the same object file, as might have been created
2064                                    by ld -r.  The only reason locals are now seen at this
2065                                    level at all is so that we can do semi-sensible things
2066                                    with parameters.  */
2067
2068                                 struct obj_symbol *nsym, **p;
2069
2070                                 nsym = arch_new_symbol();
2071                                 nsym->next = sym->next;
2072                                 nsym->ksymidx = -1;
2073
2074                                 /* Excise the old (local) symbol from the hash chain.  */
2075                                 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2076                                         continue;
2077                                 *p = sym = nsym;
2078                                 goto found;
2079                         } else if (n_binding == STB_LOCAL) {
2080                                 /* Another symbol of the same name has already been defined.
2081                                    Just add this to the local table.  */
2082                                 sym = arch_new_symbol();
2083                                 sym->next = NULL;
2084                                 sym->ksymidx = -1;
2085                                 f->local_symtab[symidx] = sym;
2086                                 goto found;
2087                         } else if (n_binding == STB_WEAK)
2088                                 return sym;
2089                         else if (o_binding == STB_WEAK)
2090                                 goto found;
2091                         /* Don't unify COMMON symbols with object types the programmer
2092                            doesn't expect.  */
2093                         else if (secidx == SHN_COMMON
2094                                         && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2095                                 return sym;
2096                         else if (o_secidx == SHN_COMMON
2097                                         && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2098                                 goto found;
2099                         else {
2100                                 /* Don't report an error if the symbol is coming from
2101                                    the kernel or some external module.  */
2102                                 if (secidx <= SHN_HIRESERVE)
2103                                         bb_error_msg("%s multiply defined", name);
2104                                 return sym;
2105                         }
2106                 }
2107
2108         /* Completely new symbol.  */
2109         sym = arch_new_symbol();
2110         sym->next = f->symtab[hash];
2111         f->symtab[hash] = sym;
2112         sym->ksymidx = -1;
2113
2114         if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2115                 if (symidx >= f->local_symtab_size)
2116                         bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2117                                         name, (long) symidx, (long) f->local_symtab_size);
2118                 else
2119                         f->local_symtab[symidx] = sym;
2120         }
2121
2122 found:
2123         sym->name = name;
2124         sym->value = value;
2125         sym->size = size;
2126         sym->secidx = secidx;
2127         sym->info = info;
2128
2129         return sym;
2130 }
2131
2132 static struct obj_symbol *
2133 obj_find_symbol(struct obj_file *f, const char *name)
2134 {
2135         struct obj_symbol *sym;
2136         unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2137
2138         for (sym = f->symtab[hash]; sym; sym = sym->next)
2139                 if (f->symbol_cmp(sym->name, name) == 0)
2140                         return sym;
2141
2142         return NULL;
2143 }
2144
2145 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2146 {
2147         if (sym) {
2148                 if (sym->secidx >= SHN_LORESERVE)
2149                         return sym->value;
2150
2151                 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2152         } else {
2153                 /* As a special case, a NULL sym has value zero.  */
2154                 return 0;
2155         }
2156 }
2157
2158 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2159 {
2160         int i, n = f->header.e_shnum;
2161
2162         for (i = 0; i < n; ++i)
2163                 if (strcmp(f->sections[i]->name, name) == 0)
2164                         return f->sections[i];
2165
2166         return NULL;
2167 }
2168
2169 static int obj_load_order_prio(struct obj_section *a)
2170 {
2171         unsigned long af, ac;
2172
2173         af = a->header.sh_flags;
2174
2175         ac = 0;
2176         if (a->name[0] != '.' || strlen(a->name) != 10 ||
2177                         strcmp(a->name + 5, ".init"))
2178                 ac |= 32;
2179         if (af & SHF_ALLOC)
2180                 ac |= 16;
2181         if (!(af & SHF_WRITE))
2182                 ac |= 8;
2183         if (af & SHF_EXECINSTR)
2184                 ac |= 4;
2185         if (a->header.sh_type != SHT_NOBITS)
2186                 ac |= 2;
2187
2188         return ac;
2189 }
2190
2191 static void
2192 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2193 {
2194         struct obj_section **p;
2195         int prio = obj_load_order_prio(sec);
2196         for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2197                 if (obj_load_order_prio(*p) < prio)
2198                         break;
2199         sec->load_next = *p;
2200         *p = sec;
2201 }
2202
2203 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2204                                 const char *name,
2205                                 unsigned long align,
2206                                 unsigned long size)
2207 {
2208         int newidx = f->header.e_shnum++;
2209         struct obj_section *sec;
2210
2211         f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2212         f->sections[newidx] = sec = arch_new_section();
2213
2214         memset(sec, 0, sizeof(*sec));
2215         sec->header.sh_type = SHT_PROGBITS;
2216         sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2217         sec->header.sh_size = size;
2218         sec->header.sh_addralign = align;
2219         sec->name = name;
2220         sec->idx = newidx;
2221         if (size)
2222                 sec->contents = xmalloc(size);
2223
2224         obj_insert_section_load_order(f, sec);
2225
2226         return sec;
2227 }
2228
2229 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2230                                 const char *name,
2231                                 unsigned long align,
2232                                 unsigned long size)
2233 {
2234         int newidx = f->header.e_shnum++;
2235         struct obj_section *sec;
2236
2237         f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2238         f->sections[newidx] = sec = arch_new_section();
2239
2240         memset(sec, 0, sizeof(*sec));
2241         sec->header.sh_type = SHT_PROGBITS;
2242         sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2243         sec->header.sh_size = size;
2244         sec->header.sh_addralign = align;
2245         sec->name = name;
2246         sec->idx = newidx;
2247         if (size)
2248                 sec->contents = xmalloc(size);
2249
2250         sec->load_next = f->load_order;
2251         f->load_order = sec;
2252         if (f->load_order_search_start == &f->load_order)
2253                 f->load_order_search_start = &sec->load_next;
2254
2255         return sec;
2256 }
2257
2258 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2259 {
2260         unsigned long oldsize = sec->header.sh_size;
2261         if (more) {
2262                 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2263         }
2264         return sec->contents + oldsize;
2265 }
2266
2267
2268 /* Conditionally add the symbols from the given symbol set to the
2269    new module.  */
2270
2271 static int
2272 add_symbols_from( struct obj_file *f,
2273                                  int idx, struct new_module_symbol *syms, size_t nsyms)
2274 {
2275         struct new_module_symbol *s;
2276         size_t i;
2277         int used = 0;
2278 #ifdef SYMBOL_PREFIX
2279         char *name_buf = 0;
2280         size_t name_alloced_size = 0;
2281 #endif
2282 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2283         int gpl;
2284
2285         gpl = obj_gpl_license(f, NULL) == 0;
2286 #endif
2287         for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2288                 /* Only add symbols that are already marked external.
2289                    If we override locals we may cause problems for
2290                    argument initialization.  We will also create a false
2291                    dependency on the module.  */
2292                 struct obj_symbol *sym;
2293                 char *name;
2294
2295                 /* GPL licensed modules can use symbols exported with
2296                  * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2297                  * exported names.  Non-GPL modules never see any GPLONLY_
2298                  * symbols so they cannot fudge it by adding the prefix on
2299                  * their references.
2300                  */
2301                 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2302 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2303                         if (gpl)
2304                                 s->name += 8;
2305                         else
2306 #endif
2307                                 continue;
2308                 }
2309                 name = (char *)s->name;
2310
2311 #ifdef SYMBOL_PREFIX
2312                 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2313                    kernel exports `C names', but module object files
2314                    reference `linker names').  */
2315                 size_t extra = sizeof SYMBOL_PREFIX;
2316                 size_t name_size = strlen (name) + extra;
2317                 if (name_size > name_alloced_size) {
2318                         name_alloced_size = name_size * 2;
2319                         name_buf = alloca (name_alloced_size);
2320                 }
2321                 strcpy (name_buf, SYMBOL_PREFIX);
2322                 strcpy (name_buf + extra - 1, name);
2323                 name = name_buf;
2324 #endif /* SYMBOL_PREFIX */
2325
2326                 sym = obj_find_symbol(f, name);
2327                 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2328 #ifdef SYMBOL_PREFIX
2329                         /* Put NAME_BUF into more permanent storage.  */
2330                         name = xmalloc (name_size);
2331                         strcpy (name, name_buf);
2332 #endif
2333                         sym = obj_add_symbol(f, name, -1,
2334                                         ELF_ST_INFO(STB_GLOBAL,
2335                                                 STT_NOTYPE),
2336                                         idx, s->value, 0);
2337                         /* Did our symbol just get installed?  If so, mark the
2338                            module as "used".  */
2339                         if (sym->secidx == idx)
2340                                 used = 1;
2341                 }
2342         }
2343
2344         return used;
2345 }
2346
2347 static void add_kernel_symbols(struct obj_file *f)
2348 {
2349         struct external_module *m;
2350         int i, nused = 0;
2351
2352         /* Add module symbols first.  */
2353
2354         for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2355                 if (m->nsyms
2356                                 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2357                                         m->nsyms)) m->used = 1, ++nused;
2358
2359         n_ext_modules_used = nused;
2360
2361         /* And finally the symbols from the kernel proper.  */
2362
2363         if (nksyms)
2364                 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2365 }
2366
2367 static char *get_modinfo_value(struct obj_file *f, const char *key)
2368 {
2369         struct obj_section *sec;
2370         char *p, *v, *n, *ep;
2371         size_t klen = strlen(key);
2372
2373         sec = obj_find_section(f, ".modinfo");
2374         if (sec == NULL)
2375                 return NULL;
2376         p = sec->contents;
2377         ep = p + sec->header.sh_size;
2378         while (p < ep) {
2379                 v = strchr(p, '=');
2380                 n = strchr(p, '\0');
2381                 if (v) {
2382                         if (p + klen == v && strncmp(p, key, klen) == 0)
2383                                 return v + 1;
2384                 } else {
2385                         if (p + klen == n && strcmp(p, key) == 0)
2386                                 return n;
2387                 }
2388                 p = n + 1;
2389         }
2390
2391         return NULL;
2392 }
2393
2394
2395 /*======================================================================*/
2396 /* Functions relating to module loading after 2.1.18.  */
2397
2398 static int
2399 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2400 {
2401         while (argc > 0) {
2402                 char *p, *q, *key, *sym_name;
2403                 struct obj_symbol *sym;
2404                 char *contents, *loc;
2405                 int min, max, n;
2406
2407                 p = *argv;
2408                 if ((q = strchr(p, '=')) == NULL) {
2409                         argc--;
2410                         continue;
2411                 }
2412
2413                 key = alloca(q - p + 6);
2414                 memcpy(key, "parm_", 5);
2415                 memcpy(key + 5, p, q - p);
2416                 key[q - p + 5] = 0;
2417
2418                 p = get_modinfo_value(f, key);
2419                 key += 5;
2420                 if (p == NULL) {
2421                         bb_error_msg("invalid parameter %s", key);
2422                         return 0;
2423                 }
2424
2425 #ifdef SYMBOL_PREFIX
2426                 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2427                 strcpy (sym_name, SYMBOL_PREFIX);
2428                 strcat (sym_name, key);
2429 #else
2430                 sym_name = key;
2431 #endif
2432                 sym = obj_find_symbol(f, sym_name);
2433
2434                 /* Also check that the parameter was not resolved from the kernel.  */
2435                 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2436                         bb_error_msg("symbol for parameter %s not found", key);
2437                         return 0;
2438                 }
2439
2440                 if (isdigit(*p)) {
2441                         min = strtoul(p, &p, 10);
2442                         if (*p == '-')
2443                                 max = strtoul(p + 1, &p, 10);
2444                         else
2445                                 max = min;
2446                 } else
2447                         min = max = 1;
2448
2449                 contents = f->sections[sym->secidx]->contents;
2450                 loc = contents + sym->value;
2451                 n = (*++q != '\0');
2452
2453                 while (1) {
2454                         if ((*p == 's') || (*p == 'c')) {
2455                                 char *str;
2456
2457                                 /* Do C quoting if we begin with a ", else slurp the lot.  */
2458                                 if (*q == '"') {
2459                                         char *r;
2460
2461                                         str = alloca(strlen(q));
2462                                         for (r = str, q++; *q != '"'; ++q, ++r) {
2463                                                 if (*q == '\0') {
2464                                                         bb_error_msg("improperly terminated string argument for %s",
2465                                                                         key);
2466                                                         return 0;
2467                                                 } else if (*q == '\\')
2468                                                         switch (*++q) {
2469                                                         case 'a':
2470                                                                 *r = '\a';
2471                                                                 break;
2472                                                         case 'b':
2473                                                                 *r = '\b';
2474                                                                 break;
2475                                                         case 'e':
2476                                                                 *r = '\033';
2477                                                                 break;
2478                                                         case 'f':
2479                                                                 *r = '\f';
2480                                                                 break;
2481                                                         case 'n':
2482                                                                 *r = '\n';
2483                                                                 break;
2484                                                         case 'r':
2485                                                                 *r = '\r';
2486                                                                 break;
2487                                                         case 't':
2488                                                                 *r = '\t';
2489                                                                 break;
2490
2491                                                         case '0':
2492                                                         case '1':
2493                                                         case '2':
2494                                                         case '3':
2495                                                         case '4':
2496                                                         case '5':
2497                                                         case '6':
2498                                                         case '7':
2499                                                                 {
2500                                                                         int c = *q - '0';
2501                                                                         if (q[1] >= '0' && q[1] <= '7') {
2502                                                                                 c = (c * 8) + *++q - '0';
2503                                                                                 if (q[1] >= '0' && q[1] <= '7')
2504                                                                                         c = (c * 8) + *++q - '0';
2505                                                                         }
2506                                                                         *r = c;
2507                                                                 }
2508                                                                 break;
2509
2510                                                         default:
2511                                                                 *r = *q;
2512                                                                 break;
2513                                                         } else
2514                                                                 *r = *q;
2515                                         }
2516                                         *r = '\0';
2517                                         ++q;
2518                                 } else {
2519                                         char *r;
2520
2521                                         /* In this case, the string is not quoted. We will break
2522                                            it using the coma (like for ints). If the user wants to
2523                                            include comas in a string, he just has to quote it */
2524
2525                                         /* Search the next coma */
2526                                         r = strchr(q, ',');
2527
2528                                         /* Found ? */
2529                                         if (r != (char *) NULL) {
2530                                                 /* Recopy the current field */
2531                                                 str = alloca(r - q + 1);
2532                                                 memcpy(str, q, r - q);
2533
2534                                                 /* I don't know if it is useful, as the previous case
2535                                                    doesn't nul terminate the string ??? */
2536                                                 str[r - q] = '\0';
2537
2538                                                 /* Keep next fields */
2539                                                 q = r;
2540                                         } else {
2541                                                 /* last string */
2542                                                 str = q;
2543                                                 q = (char*)"";
2544                                         }
2545                                 }
2546
2547                                 if (*p == 's') {
2548                                         /* Normal string */
2549                                         obj_string_patch(f, sym->secidx, loc - contents, str);
2550                                         loc += tgt_sizeof_char_p;
2551                                 } else {
2552                                         /* Array of chars (in fact, matrix !) */
2553                                         unsigned long charssize;        /* size of each member */
2554
2555                                         /* Get the size of each member */
2556                                         /* Probably we should do that outside the loop ? */
2557                                         if (!isdigit(*(p + 1))) {
2558                                                 bb_error_msg("parameter type 'c' for %s must be followed by"
2559                                                                 " the maximum size", key);
2560                                                 return 0;
2561                                         }
2562                                         charssize = strtoul(p + 1, (char **) NULL, 10);
2563
2564                                         /* Check length */
2565                                         if (strlen(str) >= charssize) {
2566                                                 bb_error_msg("string too long for %s (max %ld)", key,
2567                                                                 charssize - 1);
2568                                                 return 0;
2569                                         }
2570
2571                                         /* Copy to location */
2572                                         strcpy((char *) loc, str);
2573                                         loc += charssize;
2574                                 }
2575                         } else {
2576                                 long v = strtoul(q, &q, 0);
2577                                 switch (*p) {
2578                                 case 'b':
2579                                         *loc++ = v;
2580                                         break;
2581                                 case 'h':
2582                                         *(short *) loc = v;
2583                                         loc += tgt_sizeof_short;
2584                                         break;
2585                                 case 'i':
2586                                         *(int *) loc = v;
2587                                         loc += tgt_sizeof_int;
2588                                         break;
2589                                 case 'l':
2590                                         *(long *) loc = v;
2591                                         loc += tgt_sizeof_long;
2592                                         break;
2593
2594                                 default:
2595                                         bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2596                                         return 0;
2597                                 }
2598                         }
2599
2600 retry_end_of_value:
2601                         switch (*q) {
2602                         case '\0':
2603                                 goto end_of_arg;
2604
2605                         case ' ':
2606                         case '\t':
2607                         case '\n':
2608                         case '\r':
2609                                 ++q;
2610                                 goto retry_end_of_value;
2611
2612                         case ',':
2613                                 if (++n > max) {
2614                                         bb_error_msg("too many values for %s (max %d)", key, max);
2615                                         return 0;
2616                                 }
2617                                 ++q;
2618                                 break;
2619
2620                         default:
2621                                 bb_error_msg("invalid argument syntax for %s", key);
2622                                 return 0;
2623                         }
2624                 }
2625
2626 end_of_arg:
2627                 if (n < min) {
2628                         bb_error_msg("too few values for %s (min %d)", key, min);
2629                         return 0;
2630                 }
2631
2632                 argc--, argv++;
2633         }
2634
2635         return 1;
2636 }
2637
2638 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2639 static int new_is_module_checksummed(struct obj_file *f)
2640 {
2641         const char *p = get_modinfo_value(f, "using_checksums");
2642         if (p)
2643                 return xatoi(p);
2644         else
2645                 return 0;
2646 }
2647
2648 /* Get the module's kernel version in the canonical integer form.  */
2649
2650 static int
2651 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2652 {
2653         char *p, *q;
2654         int a, b, c;
2655
2656         p = get_modinfo_value(f, "kernel_version");
2657         if (p == NULL)
2658                 return -1;
2659         safe_strncpy(str, p, STRVERSIONLEN);
2660
2661         a = strtoul(p, &p, 10);
2662         if (*p != '.')
2663                 return -1;
2664         b = strtoul(p + 1, &p, 10);
2665         if (*p != '.')
2666                 return -1;
2667         c = strtoul(p + 1, &q, 10);
2668         if (p + 1 == q)
2669                 return -1;
2670
2671         return a << 16 | b << 8 | c;
2672 }
2673
2674 #endif   /* FEATURE_INSMOD_VERSION_CHECKING */
2675
2676
2677 /* Fetch the loaded modules, and all currently exported symbols.  */
2678
2679 static int new_get_kernel_symbols(void)
2680 {
2681         char *module_names, *mn;
2682         struct external_module *modules, *m;
2683         struct new_module_symbol *syms, *s;
2684         size_t ret, bufsize, nmod, nsyms, i, j;
2685
2686         /* Collect the loaded modules.  */
2687
2688         module_names = xmalloc(bufsize = 256);
2689 retry_modules_load:
2690         if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2691                 if (errno == ENOSPC && bufsize < ret) {
2692                         module_names = xrealloc(module_names, bufsize = ret);
2693                         goto retry_modules_load;
2694                 }
2695                 bb_perror_msg("QM_MODULES");
2696                 return 0;
2697         }
2698
2699         n_ext_modules = nmod = ret;
2700
2701         /* Collect the modules' symbols.  */
2702
2703         if (nmod) {
2704                 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2705                 memset(modules, 0, nmod * sizeof(*modules));
2706                 for (i = 0, mn = module_names, m = modules;
2707                                 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2708                         struct new_module_info info;
2709
2710                         if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2711                                 if (errno == ENOENT) {
2712                                         /* The module was removed out from underneath us.  */
2713                                         continue;
2714                                 }
2715                                 bb_perror_msg("query_module: QM_INFO: %s", mn);
2716                                 return 0;
2717                         }
2718
2719                         syms = xmalloc(bufsize = 1024);
2720 retry_mod_sym_load:
2721                         if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2722                                 switch (errno) {
2723                                         case ENOSPC:
2724                                                 syms = xrealloc(syms, bufsize = ret);
2725                                                 goto retry_mod_sym_load;
2726                                         case ENOENT:
2727                                                 /* The module was removed out from underneath us.  */
2728                                                 continue;
2729                                         default:
2730                                                 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2731                                                 return 0;
2732                                 }
2733                         }
2734                         nsyms = ret;
2735
2736                         m->name = mn;
2737                         m->addr = info.addr;
2738                         m->nsyms = nsyms;
2739                         m->syms = syms;
2740
2741                         for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2742                                 s->name += (unsigned long) syms;
2743                         }
2744                 }
2745         }
2746
2747         /* Collect the kernel's symbols.  */
2748
2749         syms = xmalloc(bufsize = 16 * 1024);
2750 retry_kern_sym_load:
2751         if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2752                 if (errno == ENOSPC && bufsize < ret) {
2753                         syms = xrealloc(syms, bufsize = ret);
2754                         goto retry_kern_sym_load;
2755                 }
2756                 bb_perror_msg("kernel: QM_SYMBOLS");
2757                 return 0;
2758         }
2759         nksyms = nsyms = ret;
2760         ksyms = syms;
2761
2762         for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2763                 s->name += (unsigned long) syms;
2764         }
2765         return 1;
2766 }
2767
2768
2769 /* Return the kernel symbol checksum version, or zero if not used.  */
2770
2771 static int new_is_kernel_checksummed(void)
2772 {
2773         struct new_module_symbol *s;
2774         size_t i;
2775
2776         /* Using_Versions is not the first symbol, but it should be in there.  */
2777
2778         for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2779                 if (strcmp((char *) s->name, "Using_Versions") == 0)
2780                         return s->value;
2781
2782         return 0;
2783 }
2784
2785
2786 static int new_create_this_module(struct obj_file *f, const char *m_name)
2787 {
2788         struct obj_section *sec;
2789
2790         sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2791                         sizeof(struct new_module));
2792         memset(sec->contents, 0, sizeof(struct new_module));
2793
2794         obj_add_symbol(f, SPFX "__this_module", -1,
2795                         ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2796                         sizeof(struct new_module));
2797
2798         obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2799                         m_name);
2800
2801         return 1;
2802 }
2803
2804 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2805 /* add an entry to the __ksymtab section, creating it if necessary */
2806 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2807 {
2808         struct obj_section *sec;
2809         ElfW(Addr) ofs;
2810
2811         /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2812          * If __ksymtab is defined but not marked alloc, x out the first character
2813          * (no obj_delete routine) and create a new __ksymtab with the correct
2814          * characteristics.
2815          */
2816         sec = obj_find_section(f, "__ksymtab");
2817         if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2818                 *((char *)(sec->name)) = 'x';   /* override const */
2819                 sec = NULL;
2820         }
2821         if (!sec)
2822                 sec = obj_create_alloced_section(f, "__ksymtab",
2823                                 tgt_sizeof_void_p, 0);
2824         if (!sec)
2825                 return;
2826         sec->header.sh_flags |= SHF_ALLOC;
2827         /* Empty section might be byte-aligned */
2828         sec->header.sh_addralign = tgt_sizeof_void_p;
2829         ofs = sec->header.sh_size;
2830         obj_symbol_patch(f, sec->idx, ofs, sym);
2831         obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2832         obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2833 }
2834 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2835
2836 static int new_create_module_ksymtab(struct obj_file *f)
2837 {
2838         struct obj_section *sec;
2839         int i;
2840
2841         /* We must always add the module references.  */
2842
2843         if (n_ext_modules_used) {
2844                 struct new_module_ref *dep;
2845                 struct obj_symbol *tm;
2846
2847                 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2848                                 (sizeof(struct new_module_ref)
2849                                  * n_ext_modules_used));
2850                 if (!sec)
2851                         return 0;
2852
2853                 tm = obj_find_symbol(f, SPFX "__this_module");
2854                 dep = (struct new_module_ref *) sec->contents;
2855                 for (i = 0; i < n_ext_modules; ++i)
2856                         if (ext_modules[i].used) {
2857                                 dep->dep = ext_modules[i].addr;
2858                                 obj_symbol_patch(f, sec->idx,
2859                                                 (char *) &dep->ref - sec->contents, tm);
2860                                 dep->next_ref = 0;
2861                                 ++dep;
2862                         }
2863         }
2864
2865         if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2866                 size_t nsyms;
2867                 int *loaded;
2868
2869                 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2870
2871                 /* We don't want to export symbols residing in sections that
2872                    aren't loaded.  There are a number of these created so that
2873                    we make sure certain module options don't appear twice.  */
2874
2875                 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2876                 while (--i >= 0)
2877                         loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2878
2879                 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2880                         struct obj_symbol *sym;
2881                         for (sym = f->symtab[i]; sym; sym = sym->next)
2882                                 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2883                                                 && sym->secidx <= SHN_HIRESERVE
2884                                                 && (sym->secidx >= SHN_LORESERVE
2885                                                         || loaded[sym->secidx])) {
2886                                         ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2887
2888                                         obj_symbol_patch(f, sec->idx, ofs, sym);
2889                                         obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2890                                                         sym->name);
2891
2892                                         nsyms++;
2893                                 }
2894                 }
2895
2896                 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2897         }
2898
2899         return 1;
2900 }
2901
2902
2903 static int
2904 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2905 {
2906         struct new_module *module;
2907         struct obj_section *sec;
2908         void *image;
2909         int ret;
2910         tgt_long m_addr;
2911
2912         sec = obj_find_section(f, ".this");
2913         if (!sec || !sec->contents) {
2914                 bb_perror_msg_and_die("corrupt module %s?",m_name);
2915         }
2916         module = (struct new_module *) sec->contents;
2917         m_addr = sec->header.sh_addr;
2918
2919         module->size_of_struct = sizeof(*module);
2920         module->size = m_size;
2921         module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2922
2923         sec = obj_find_section(f, "__ksymtab");
2924         if (sec && sec->header.sh_size) {
2925                 module->syms = sec->header.sh_addr;
2926                 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2927         }
2928
2929         if (n_ext_modules_used) {
2930                 sec = obj_find_section(f, ".kmodtab");
2931                 module->deps = sec->header.sh_addr;
2932                 module->ndeps = n_ext_modules_used;
2933         }
2934
2935         module->init =
2936                 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2937         module->cleanup =
2938                 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2939
2940         sec = obj_find_section(f, "__ex_table");
2941         if (sec) {
2942                 module->ex_table_start = sec->header.sh_addr;
2943                 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2944         }
2945
2946         sec = obj_find_section(f, ".text.init");
2947         if (sec) {
2948                 module->runsize = sec->header.sh_addr - m_addr;
2949         }
2950         sec = obj_find_section(f, ".data.init");
2951         if (sec) {
2952                 if (!module->runsize ||
2953                                 module->runsize > sec->header.sh_addr - m_addr)
2954                         module->runsize = sec->header.sh_addr - m_addr;
2955         }
2956         sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2957         if (sec && sec->header.sh_size) {
2958                 module->archdata_start = (void*)sec->header.sh_addr;
2959                 module->archdata_end = module->archdata_start + sec->header.sh_size;
2960         }
2961         sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2962         if (sec && sec->header.sh_size) {
2963                 module->kallsyms_start = (void*)sec->header.sh_addr;
2964                 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2965         }
2966
2967         /* Whew!  All of the initialization is complete.  Collect the final
2968            module image and give it to the kernel.  */
2969
2970         image = xmalloc(m_size);
2971         obj_create_image(f, image);
2972
2973         ret = init_module(m_name, (struct new_module *) image);
2974         if (ret)
2975                 bb_perror_msg("init_module: %s", m_name);
2976
2977         free(image);
2978
2979         return ret == 0;
2980 }
2981
2982
2983 /*======================================================================*/
2984
2985 static int
2986 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2987                                  const char *string)
2988 {
2989         struct obj_string_patch *p;
2990         struct obj_section *strsec;
2991         size_t len = strlen(string) + 1;
2992         char *loc;
2993
2994         p = xmalloc(sizeof(*p));
2995         p->next = f->string_patches;
2996         p->reloc_secidx = secidx;
2997         p->reloc_offset = offset;
2998         f->string_patches = p;
2999
3000         strsec = obj_find_section(f, ".kstrtab");
3001         if (strsec == NULL) {
3002                 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3003                 p->string_offset = 0;
3004                 loc = strsec->contents;
3005         } else {
3006                 p->string_offset = strsec->header.sh_size;
3007                 loc = obj_extend_section(strsec, len);
3008         }
3009         memcpy(loc, string, len);
3010
3011         return 1;
3012 }
3013
3014 static int
3015 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3016                                  struct obj_symbol *sym)
3017 {
3018         struct obj_symbol_patch *p;
3019
3020         p = xmalloc(sizeof(*p));
3021         p->next = f->symbol_patches;
3022         p->reloc_secidx = secidx;
3023         p->reloc_offset = offset;
3024         p->sym = sym;
3025         f->symbol_patches = p;
3026
3027         return 1;
3028 }
3029
3030 static int obj_check_undefineds(struct obj_file *f)
3031 {
3032         unsigned long i;
3033         int ret = 1;
3034
3035         for (i = 0; i < HASH_BUCKETS; ++i) {
3036                 struct obj_symbol *sym;
3037                 for (sym = f->symtab[i]; sym; sym = sym->next)
3038                         if (sym->secidx == SHN_UNDEF) {
3039                                 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3040                                         sym->secidx = SHN_ABS;
3041                                         sym->value = 0;
3042                                 } else {
3043                                         if (!flag_quiet) {
3044                                                 bb_error_msg("unresolved symbol %s", sym->name);
3045                                         }
3046                                         ret = 0;
3047                                 }
3048                         }
3049         }
3050
3051         return ret;
3052 }
3053
3054 static void obj_allocate_commons(struct obj_file *f)
3055 {
3056         struct common_entry {
3057                 struct common_entry *next;
3058                 struct obj_symbol *sym;
3059         } *common_head = NULL;
3060
3061         unsigned long i;
3062
3063         for (i = 0; i < HASH_BUCKETS; ++i) {
3064                 struct obj_symbol *sym;
3065                 for (sym = f->symtab[i]; sym; sym = sym->next)
3066                         if (sym->secidx == SHN_COMMON) {
3067                                 /* Collect all COMMON symbols and sort them by size so as to
3068                                    minimize space wasted by alignment requirements.  */
3069                                 {
3070                                         struct common_entry **p, *n;
3071                                         for (p = &common_head; *p; p = &(*p)->next)
3072                                                 if (sym->size <= (*p)->sym->size)
3073                                                         break;
3074
3075                                         n = alloca(sizeof(*n));
3076                                         n->next = *p;
3077                                         n->sym = sym;
3078                                         *p = n;
3079                                 }
3080                         }
3081         }
3082
3083         for (i = 1; i < f->local_symtab_size; ++i) {
3084                 struct obj_symbol *sym = f->local_symtab[i];
3085                 if (sym && sym->secidx == SHN_COMMON) {
3086                         struct common_entry **p, *n;
3087                         for (p = &common_head; *p; p = &(*p)->next)
3088                                 if (sym == (*p)->sym)
3089                                         break;
3090                                 else if (sym->size < (*p)->sym->size) {
3091                                         n = alloca(sizeof(*n));
3092                                         n->next = *p;
3093                                         n->sym = sym;
3094                                         *p = n;
3095                                         break;
3096                                 }
3097                 }
3098         }
3099
3100         if (common_head) {
3101                 /* Find the bss section.  */
3102                 for (i = 0; i < f->header.e_shnum; ++i)
3103                         if (f->sections[i]->header.sh_type == SHT_NOBITS)
3104                                 break;
3105
3106                 /* If for some reason there hadn't been one, create one.  */
3107                 if (i == f->header.e_shnum) {
3108                         struct obj_section *sec;
3109
3110                         f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3111                         f->sections[i] = sec = arch_new_section();
3112                         f->header.e_shnum = i + 1;
3113
3114                         memset(sec, 0, sizeof(*sec));
3115                         sec->header.sh_type = SHT_PROGBITS;
3116                         sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3117                         sec->name = ".bss";
3118                         sec->idx = i;
3119                 }
3120
3121                 /* Allocate the COMMONS.  */
3122                 {
3123                         ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3124                         ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3125                         struct common_entry *c;
3126
3127                         for (c = common_head; c; c = c->next) {
3128                                 ElfW(Addr) align = c->sym->value;
3129
3130                                 if (align > max_align)
3131                                         max_align = align;
3132                                 if (bss_size & (align - 1))
3133                                         bss_size = (bss_size | (align - 1)) + 1;
3134
3135                                 c->sym->secidx = i;
3136                                 c->sym->value = bss_size;
3137
3138                                 bss_size += c->sym->size;
3139                         }
3140
3141                         f->sections[i]->header.sh_size = bss_size;
3142                         f->sections[i]->header.sh_addralign = max_align;
3143                 }
3144         }
3145
3146         /* For the sake of patch relocation and parameter initialization,
3147            allocate zeroed data for NOBITS sections now.  Note that after
3148            this we cannot assume NOBITS are really empty.  */
3149         for (i = 0; i < f->header.e_shnum; ++i) {
3150                 struct obj_section *s = f->sections[i];
3151                 if (s->header.sh_type == SHT_NOBITS) {
3152                         if (s->header.sh_size != 0)
3153                                 s->contents = memset(xmalloc(s->header.sh_size),
3154                                                 0, s->header.sh_size);
3155                         else
3156                                 s->contents = NULL;
3157
3158                         s->header.sh_type = SHT_PROGBITS;
3159                 }
3160         }
3161 }
3162
3163 static unsigned long obj_load_size(struct obj_file *f)
3164 {
3165         unsigned long dot = 0;
3166         struct obj_section *sec;
3167
3168         /* Finalize the positions of the sections relative to one another.  */
3169
3170         for (sec = f->load_order; sec; sec = sec->load_next) {
3171                 ElfW(Addr) align;
3172
3173                 align = sec->header.sh_addralign;
3174                 if (align && (dot & (align - 1)))
3175                         dot = (dot | (align - 1)) + 1;
3176
3177                 sec->header.sh_addr = dot;
3178                 dot += sec->header.sh_size;
3179         }
3180
3181         return dot;
3182 }
3183
3184 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3185 {
3186         int i, n = f->header.e_shnum;
3187         int ret = 1;
3188
3189         /* Finalize the addresses of the sections.  */
3190
3191         f->baseaddr = base;
3192         for (i = 0; i < n; ++i)
3193                 f->sections[i]->header.sh_addr += base;
3194
3195         /* And iterate over all of the relocations.  */
3196
3197         for (i = 0; i < n; ++i) {
3198                 struct obj_section *relsec, *symsec, *targsec, *strsec;
3199                 ElfW(RelM) * rel, *relend;
3200                 ElfW(Sym) * symtab;
3201                 const char *strtab;
3202
3203                 relsec = f->sections[i];
3204                 if (relsec->header.sh_type != SHT_RELM)
3205                         continue;
3206
3207                 symsec = f->sections[relsec->header.sh_link];
3208                 targsec = f->sections[relsec->header.sh_info];
3209                 strsec = f->sections[symsec->header.sh_link];
3210
3211                 rel = (ElfW(RelM) *) relsec->contents;
3212                 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3213                 symtab = (ElfW(Sym) *) symsec->contents;
3214                 strtab = (const char *) strsec->contents;
3215
3216                 for (; rel < relend; ++rel) {
3217                         ElfW(Addr) value = 0;
3218                         struct obj_symbol *intsym = NULL;
3219                         unsigned long symndx;
3220                         ElfW(Sym) * extsym = 0;
3221                         const char *errmsg;
3222
3223                         /* Attempt to find a value to use for this relocation.  */
3224
3225                         symndx = ELF_R_SYM(rel->r_info);
3226                         if (symndx) {
3227                                 /* Note we've already checked for undefined symbols.  */
3228
3229                                 extsym = &symtab[symndx];
3230                                 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3231                                         /* Local symbols we look up in the local table to be sure
3232                                            we get the one that is really intended.  */
3233                                         intsym = f->local_symtab[symndx];
3234                                 } else {
3235                                         /* Others we look up in the hash table.  */
3236                                         const char *name;
3237                                         if (extsym->st_name)
3238                                                 name = strtab + extsym->st_name;
3239                                         else
3240                                                 name = f->sections[extsym->st_shndx]->name;
3241                                         intsym = obj_find_symbol(f, name);
3242                                 }
3243
3244                                 value = obj_symbol_final_value(f, intsym);
3245                                 intsym->referenced = 1;
3246                         }
3247 #if SHT_RELM == SHT_RELA
3248 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3249                         /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9.  */
3250                         if (!extsym || !extsym->st_name ||
3251                                         ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3252 #endif
3253                                 value += rel->r_addend;
3254 #endif
3255
3256                         /* Do it! */
3257                         switch (arch_apply_relocation
3258                                         (f, targsec, symsec, intsym, rel, value)
3259                         ) {
3260                         case obj_reloc_ok:
3261                                 break;
3262
3263                         case obj_reloc_overflow:
3264                                 errmsg = "Relocation overflow";
3265                                 goto bad_reloc;
3266                         case obj_reloc_dangerous:
3267                                 errmsg = "Dangerous relocation";
3268                                 goto bad_reloc;
3269                         case obj_reloc_unhandled:
3270                                 errmsg = "Unhandled relocation";
3271 bad_reloc:
3272                                 if (extsym) {
3273                                         bb_error_msg("%s of type %ld for %s", errmsg,
3274                                                         (long) ELF_R_TYPE(rel->r_info),
3275                                                         strtab + extsym->st_name);
3276                                 } else {
3277                                         bb_error_msg("%s of type %ld", errmsg,
3278                                                         (long) ELF_R_TYPE(rel->r_info));
3279                                 }
3280                                 ret = 0;
3281                                 break;
3282                         }
3283                 }
3284         }
3285
3286         /* Finally, take care of the patches.  */
3287
3288         if (f->string_patches) {
3289                 struct obj_string_patch *p;
3290                 struct obj_section *strsec;
3291                 ElfW(Addr) strsec_base;
3292                 strsec = obj_find_section(f, ".kstrtab");
3293                 strsec_base = strsec->header.sh_addr;
3294
3295                 for (p = f->string_patches; p; p = p->next) {
3296                         struct obj_section *targsec = f->sections[p->reloc_secidx];
3297                         *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3298                                 = strsec_base + p->string_offset;
3299                 }
3300         }
3301
3302         if (f->symbol_patches) {
3303                 struct obj_symbol_patch *p;
3304
3305                 for (p = f->symbol_patches; p; p = p->next) {
3306                         struct obj_section *targsec = f->sections[p->reloc_secidx];
3307                         *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3308                                 = obj_symbol_final_value(f, p->sym);
3309                 }
3310         }
3311
3312         return ret;
3313 }
3314
3315 static int obj_create_image(struct obj_file *f, char *image)
3316 {
3317         struct obj_section *sec;
3318         ElfW(Addr) base = f->baseaddr;
3319
3320         for (sec = f->load_order; sec; sec = sec->load_next) {
3321                 char *secimg;
3322
3323                 if (sec->contents == 0 || sec->header.sh_size == 0)
3324                         continue;
3325
3326                 secimg = image + (sec->header.sh_addr - base);
3327
3328                 /* Note that we allocated data for NOBITS sections earlier.  */
3329                 memcpy(secimg, sec->contents, sec->header.sh_size);
3330         }
3331
3332         return 1;
3333 }
3334
3335 /*======================================================================*/
3336
3337 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3338 {
3339         struct obj_file *f;
3340         ElfW(Shdr) * section_headers;
3341         int shnum, i;
3342         char *shstrtab;
3343
3344         /* Read the file header.  */
3345
3346         f = arch_new_file();
3347         memset(f, 0, sizeof(*f));
3348         f->symbol_cmp = strcmp;
3349         f->symbol_hash = obj_elf_hash;
3350         f->load_order_search_start = &f->load_order;
3351
3352         fseek(fp, 0, SEEK_SET);
3353         if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3354                 bb_perror_msg("error reading ELF header");
3355                 return NULL;
3356         }
3357
3358         if (f->header.e_ident[EI_MAG0] != ELFMAG0
3359                         || f->header.e_ident[EI_MAG1] != ELFMAG1
3360                         || f->header.e_ident[EI_MAG2] != ELFMAG2
3361                         || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3362                 bb_error_msg("not an ELF file");
3363                 return NULL;
3364         }
3365         if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3366                         || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3367                                 ? ELFDATA2MSB : ELFDATA2LSB)
3368                         || f->header.e_ident[EI_VERSION] != EV_CURRENT
3369                         || !MATCH_MACHINE(f->header.e_machine)) {
3370                 bb_error_msg("ELF file not for this architecture");
3371                 return NULL;
3372         }
3373         if (f->header.e_type != ET_REL) {
3374                 bb_error_msg("ELF file not a relocatable object");
3375                 return NULL;
3376         }
3377
3378         /* Read the section headers.  */
3379
3380         if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3381                 bb_error_msg("section header size mismatch: %lu != %lu",
3382                                 (unsigned long) f->header.e_shentsize,
3383                                 (unsigned long) sizeof(ElfW(Shdr)));
3384                 return NULL;
3385         }
3386
3387         shnum = f->header.e_shnum;
3388         f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3389         memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3390
3391         section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3392         fseek(fp, f->header.e_shoff, SEEK_SET);
3393         if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3394                 bb_perror_msg("error reading ELF section headers");
3395                 return NULL;
3396         }
3397
3398         /* Read the section data.  */
3399
3400         for (i = 0; i < shnum; ++i) {
3401                 struct obj_section *sec;
3402
3403                 f->sections[i] = sec = arch_new_section();
3404                 memset(sec, 0, sizeof(*sec));
3405
3406                 sec->header = section_headers[i];
3407                 sec->idx = i;
3408
3409                 if(sec->header.sh_size) {
3410                         switch (sec->header.sh_type) {
3411                         case SHT_NULL:
3412                         case SHT_NOTE:
3413                         case SHT_NOBITS:
3414                                 /* ignore */
3415                                 break;
3416
3417                         case SHT_PROGBITS:
3418 #if LOADBITS
3419                                 if (!loadprogbits) {
3420                                         sec->contents = NULL;
3421                                         break;
3422                                 }
3423 #endif
3424                         case SHT_SYMTAB:
3425                         case SHT_STRTAB:
3426                         case SHT_RELM:
3427                                 if (sec->header.sh_size > 0) {
3428                                         sec->contents = xmalloc(sec->header.sh_size);
3429                                         fseek(fp, sec->header.sh_offset, SEEK_SET);
3430                                         if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3431                                                 bb_perror_msg("error reading ELF section data");
3432                                                 return NULL;
3433                                         }
3434                                 } else {
3435                                         sec->contents = NULL;
3436                                 }
3437                                 break;
3438
3439 #if SHT_RELM == SHT_REL
3440                         case SHT_RELA:
3441                                 bb_error_msg("RELA relocations not supported on this architecture");
3442                                 return NULL;
3443 #else
3444                         case SHT_REL:
3445                                 bb_error_msg("REL relocations not supported on this architecture");
3446                                 return NULL;
3447 #endif
3448
3449                         default:
3450                                 if (sec->header.sh_type >= SHT_LOPROC) {
3451                                         /* Assume processor specific section types are debug
3452                                            info and can safely be ignored.  If this is ever not
3453                                            the case (Hello MIPS?), don't put ifdefs here but
3454                                            create an arch_load_proc_section().  */
3455                                         break;
3456                                 }
3457
3458                                 bb_error_msg("can't handle sections of type %ld",
3459                                                 (long) sec->header.sh_type);
3460                                 return NULL;
3461                         }
3462                 }
3463         }
3464
3465         /* Do what sort of interpretation as needed by each section.  */
3466
3467         shstrtab = f->sections[f->header.e_shstrndx]->contents;
3468
3469         for (i = 0; i < shnum; ++i) {
3470                 struct obj_section *sec = f->sections[i];
3471                 sec->name = shstrtab + sec->header.sh_name;
3472         }
3473
3474         for (i = 0; i < shnum; ++i) {
3475                 struct obj_section *sec = f->sections[i];
3476
3477                 /* .modinfo should be contents only but gcc has no attribute for that.
3478                  * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3479                  */
3480                 if (strcmp(sec->name, ".modinfo") == 0)
3481                         sec->header.sh_flags &= ~SHF_ALLOC;
3482
3483                 if (sec->header.sh_flags & SHF_ALLOC)
3484                         obj_insert_section_load_order(f, sec);
3485
3486                 switch (sec->header.sh_type) {
3487                 case SHT_SYMTAB:
3488                         {
3489                                 unsigned long nsym, j;
3490                                 char *strtab;
3491                                 ElfW(Sym) * sym;
3492
3493                                 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3494                                         bb_error_msg("symbol size mismatch: %lu != %lu",
3495                                                         (unsigned long) sec->header.sh_entsize,
3496                                                         (unsigned long) sizeof(ElfW(Sym)));
3497                                         return NULL;
3498                                 }
3499
3500                                 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3501                                 strtab = f->sections[sec->header.sh_link]->contents;
3502                                 sym = (ElfW(Sym) *) sec->contents;
3503
3504                                 /* Allocate space for a table of local symbols.  */
3505                                 j = f->local_symtab_size = sec->header.sh_info;
3506                                 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3507
3508                                 /* Insert all symbols into the hash table.  */
3509                                 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3510                                         ElfW(Addr) val = sym->st_value;
3511                                         const char *name;
3512                                         if (sym->st_name)
3513                                                 name = strtab + sym->st_name;
3514                                         else if (sym->st_shndx < shnum)
3515                                                 name = f->sections[sym->st_shndx]->name;
3516                                         else
3517                                                 continue;
3518 #if defined(__SH5__)
3519                                         /*
3520                                          * For sh64 it is possible that the target of a branch
3521                                          * requires a mode switch (32 to 16 and back again).
3522                                          *
3523                                          * This is implied by the lsb being set in the target
3524                                          * address for SHmedia mode and clear for SHcompact.
3525                                          */
3526                                         val |= sym->st_other & 4;
3527 #endif
3528
3529                                         obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3530                                                         val, sym->st_size);
3531                                 }
3532                         }
3533                         break;
3534
3535                 case SHT_RELM:
3536                         if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3537                                 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3538                                                 (unsigned long) sec->header.sh_entsize,
3539                                                 (unsigned long) sizeof(ElfW(RelM)));
3540                                 return NULL;
3541                         }
3542                         break;
3543                         /* XXX  Relocation code from modutils-2.3.19 is not here.
3544                          * Why?  That's about 20 lines of code from obj/obj_load.c,
3545                          * which gets done in a second pass through the sections.
3546                          * This BusyBox insmod does similar work in obj_relocate(). */
3547                 }
3548         }
3549
3550         return f;
3551 }
3552
3553 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3554 /*
3555  * load the unloaded sections directly into the memory allocated by
3556  * kernel for the module
3557  */
3558
3559 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3560 {
3561         ElfW(Addr) base = f->baseaddr;
3562         struct obj_section* sec;
3563
3564         for (sec = f->load_order; sec; sec = sec->load_next) {
3565
3566                 /* section already loaded? */
3567                 if (sec->contents != NULL)
3568                         continue;
3569
3570                 if (sec->header.sh_size == 0)
3571                         continue;
3572
3573                 sec->contents = imagebase + (sec->header.sh_addr - base);
3574                 fseek(fp, sec->header.sh_offset, SEEK_SET);
3575                 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3576                         bb_perror_msg("error reading ELF section data");
3577                         return 0;
3578                 }
3579
3580         }
3581         return 1;
3582 }
3583 #endif
3584
3585 static void hide_special_symbols(struct obj_file *f)
3586 {
3587         static const char *const specials[] = {
3588                 SPFX "cleanup_module",
3589                 SPFX "init_module",
3590                 SPFX "kernel_version",
3591                 NULL
3592         };
3593
3594         struct obj_symbol *sym;
3595         const char *const *p;
3596
3597         for (p = specials; *p; ++p) {
3598                 sym = obj_find_symbol(f, *p);
3599                 if (sym != NULL)
3600                         sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3601         }
3602 }
3603
3604
3605 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3606 static int obj_gpl_license(struct obj_file *f, const char **license)
3607 {
3608         struct obj_section *sec;
3609         /* This list must match *exactly* the list of allowable licenses in
3610          * linux/include/linux/module.h.  Checking for leading "GPL" will not
3611          * work, somebody will use "GPL sucks, this is proprietary".
3612          */
3613         static const char * const gpl_licenses[] = {
3614                 "GPL",
3615                 "GPL v2",
3616                 "GPL and additional rights",
3617                 "Dual BSD/GPL",
3618                 "Dual MPL/GPL",
3619         };
3620
3621         sec = obj_find_section(f, ".modinfo");
3622         if (sec) {
3623                 const char *value, *ptr, *endptr;
3624                 ptr = sec->contents;
3625                 endptr = ptr + sec->header.sh_size;
3626                 while (ptr < endptr) {
3627                         value = strchr(ptr, '=');
3628                         if (value && strncmp(ptr, "license", value-ptr) == 0) {
3629                                 int i;
3630                                 if (license)
3631                                         *license = value+1;
3632                                 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3633                                         if (strcmp(value+1, gpl_licenses[i]) == 0)
3634                                                 return 0;
3635                                 }
3636                                 return 2;
3637                         }
3638                         if (strchr(ptr, '\0'))
3639                                 ptr = strchr(ptr, '\0') + 1;
3640                         else
3641                                 ptr = endptr;
3642                 }
3643         }
3644         return 1;
3645 }
3646
3647 #define TAINT_FILENAME                  "/proc/sys/kernel/tainted"
3648 #define TAINT_PROPRIETORY_MODULE        (1<<0)
3649 #define TAINT_FORCED_MODULE             (1<<1)
3650 #define TAINT_UNSAFE_SMP                (1<<2)
3651 #define TAINT_URL                       "http://www.tux.org/lkml/#export-tainted"
3652
3653 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3654                 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3655 {
3656         char buf[80];
3657         int oldval;
3658         static int first = 1;
3659         if (fd < 0 && !kernel_has_tainted)
3660                 return;         /* New modutils on old kernel */
3661         printf("Warning: loading %s will taint the kernel: %s%s\n",
3662                         m_name, text1, text2);
3663         if (first) {
3664                 printf("  See %s for information about tainted modules\n", TAINT_URL);
3665                 first = 0;
3666         }
3667         if (fd >= 0) {
3668                 read(fd, buf, sizeof(buf)-1);
3669                 buf[sizeof(buf)-1] = '\0';
3670                 oldval = strtoul(buf, NULL, 10);
3671                 sprintf(buf, "%d\n", oldval | taint);
3672                 write(fd, buf, strlen(buf));
3673         }
3674 }
3675
3676 /* Check if loading this module will taint the kernel. */
3677 static void check_tainted_module(struct obj_file *f, char *m_name)
3678 {
3679         static const char tainted_file[] = TAINT_FILENAME;
3680         int fd, kernel_has_tainted;
3681         const char *ptr;
3682
3683         kernel_has_tainted = 1;
3684         fd = open(tainted_file, O_RDWR);
3685         if (fd < 0) {
3686                 if (errno == ENOENT)
3687                         kernel_has_tainted = 0;
3688                 else if (errno == EACCES)
3689                         kernel_has_tainted = 1;
3690                 else {
3691                         perror(tainted_file);
3692                         kernel_has_tainted = 0;
3693                 }
3694         }
3695
3696         switch (obj_gpl_license(f, &ptr)) {
3697                 case 0:
3698                         break;
3699                 case 1:
3700                         set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3701                         break;
3702                 case 2:
3703                         /* The module has a non-GPL license so we pretend that the
3704                          * kernel always has a taint flag to get a warning even on
3705                          * kernels without the proc flag.
3706                          */
3707                         set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3708                         break;
3709                 default:
3710                         set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3711                         break;
3712         }
3713
3714         if (flag_force_load)
3715                 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3716
3717         if (fd >= 0)
3718                 close(fd);
3719 }
3720 #else /* FEATURE_CHECK_TAINTED_MODULE */
3721 #define check_tainted_module(x, y) do { } while (0);
3722 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3723
3724 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3725 /* add module source, timestamp, kernel version and a symbol for the
3726  * start of some sections.  this info is used by ksymoops to do better
3727  * debugging.
3728  */
3729 static int
3730 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3731 {
3732 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3733         return new_get_module_version(f, str);
3734 #else  /* FEATURE_INSMOD_VERSION_CHECKING */
3735         strncpy(str, "???", sizeof(str));
3736         return -1;
3737 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3738 }
3739
3740 /* add module source, timestamp, kernel version and a symbol for the
3741  * start of some sections.  this info is used by ksymoops to do better
3742  * debugging.
3743  */
3744 static void
3745 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3746                                  const char *m_name)
3747 {
3748         static const char symprefix[] = "__insmod_";
3749         struct obj_section *sec;
3750         struct obj_symbol *sym;
3751         char *name, *absolute_filename;
3752         char str[STRVERSIONLEN], real[PATH_MAX];
3753         int i, l, lm_name, lfilename, use_ksymtab, version;
3754         struct stat statbuf;
3755
3756         static const char *section_names[] = {
3757                 ".text",
3758                 ".rodata",
3759                 ".data",
3760                 ".bss",
3761                 ".sbss"
3762         };
3763
3764         if (realpath(filename, real)) {
3765                 absolute_filename = xstrdup(real);
3766         } else {
3767                 bb_perror_msg("cannot get realpath for %s", filename);
3768                 absolute_filename = xstrdup(filename);
3769         }
3770
3771         lm_name = strlen(m_name);
3772         lfilename = strlen(absolute_filename);
3773
3774         /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3775          * are not to be exported.  otherwise leave ksymtab alone for now, the
3776          * "export all symbols" compatibility code will export these symbols later.
3777          */
3778         use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3779
3780         sec = obj_find_section(f, ".this");
3781         if (sec) {
3782                 /* tag the module header with the object name, last modified
3783                  * timestamp and module version.  worst case for module version
3784                  * is 0xffffff, decimal 16777215.  putting all three fields in
3785                  * one symbol is less readable but saves kernel space.
3786                  */
3787                 l = sizeof(symprefix)+                  /* "__insmod_" */
3788                         lm_name+                                /* module name */
3789                         2+                                      /* "_O" */
3790                         lfilename+                              /* object filename */
3791                         2+                                      /* "_M" */
3792                         2*sizeof(statbuf.st_mtime)+             /* mtime in hex */
3793                         2+                                      /* "_V" */
3794                         8+                                      /* version in dec */
3795                         1;                                      /* nul */
3796                 name = xmalloc(l);
3797                 if (stat(absolute_filename, &statbuf) != 0)
3798                         statbuf.st_mtime = 0;
3799                 version = get_module_version(f, str);   /* -1 if not found */
3800                 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3801                                 symprefix, m_name, absolute_filename,
3802                                 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3803                                 version);
3804                 sym = obj_add_symbol(f, name, -1,
3805                                 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3806                                 sec->idx, sec->header.sh_addr, 0);
3807                 if (use_ksymtab)
3808                         new_add_ksymtab(f, sym);
3809         }
3810         free(absolute_filename);
3811 #ifdef _NOT_SUPPORTED_
3812         /* record where the persistent data is going, same address as previous symbol */
3813
3814         if (f->persist) {
3815                 l = sizeof(symprefix)+          /* "__insmod_" */
3816                         lm_name+                /* module name */
3817                         2+                      /* "_P" */
3818                         strlen(f->persist)+     /* data store */
3819                         1;                      /* nul */
3820                 name = xmalloc(l);
3821                 snprintf(name, l, "%s%s_P%s",
3822                                 symprefix, m_name, f->persist);
3823                 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3824                                 sec->idx, sec->header.sh_addr, 0);
3825                 if (use_ksymtab)
3826                         new_add_ksymtab(f, sym);
3827         }
3828 #endif /* _NOT_SUPPORTED_ */
3829         /* tag the desired sections if size is non-zero */
3830
3831         for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3832                 sec = obj_find_section(f, section_names[i]);
3833                 if (sec && sec->header.sh_size) {
3834                         l = sizeof(symprefix)+          /* "__insmod_" */
3835                                 lm_name+                /* module name */
3836                                 2+                      /* "_S" */
3837                                 strlen(sec->name)+      /* section name */
3838                                 2+                      /* "_L" */
3839                                 8+                      /* length in dec */
3840                                 1;                      /* nul */
3841                         name = xmalloc(l);
3842                         snprintf(name, l, "%s%s_S%s_L%ld",
3843                                         symprefix, m_name, sec->name,
3844                                         (long)sec->header.sh_size);
3845                         sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3846                                         sec->idx, sec->header.sh_addr, 0);
3847                         if (use_ksymtab)
3848                                 new_add_ksymtab(f, sym);
3849                 }
3850         }
3851 }
3852 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3853
3854 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3855 static void print_load_map(struct obj_file *f)
3856 {
3857         struct obj_section *sec;
3858 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3859         struct obj_symbol **all, **p;
3860         int i, nsyms, *loaded;
3861         struct obj_symbol *sym;
3862 #endif
3863         /* Report on the section layout.  */
3864
3865         printf("Sections:       Size      %-*s  Align\n",
3866                         (int) (2 * sizeof(void *)), "Address");
3867
3868         for (sec = f->load_order; sec; sec = sec->load_next) {
3869                 int a;
3870                 unsigned long tmp;
3871
3872                 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3873                         tmp >>= 1;
3874                 if (a == -1)
3875                         a = 0;
3876
3877                 printf("%-15s %08lx  %0*lx  2**%d\n",
3878                                 sec->name,
3879                                 (long)sec->header.sh_size,
3880                                 (int) (2 * sizeof(void *)),
3881                                 (long)sec->header.sh_addr,
3882                                 a);
3883         }
3884 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3885         /* Quick reference which section indicies are loaded.  */
3886
3887         loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3888         while (--i >= 0)
3889                 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3890
3891         /* Collect the symbols we'll be listing.  */
3892
3893         for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3894                 for (sym = f->symtab[i]; sym; sym = sym->next)
3895                         if (sym->secidx <= SHN_HIRESERVE
3896                                         && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3897                                 ++nsyms;
3898
3899         all = alloca(nsyms * sizeof(struct obj_symbol *));
3900
3901         for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3902                 for (sym = f->symtab[i]; sym; sym = sym->next)
3903                         if (sym->secidx <= SHN_HIRESERVE
3904                                         && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3905                                 *p++ = sym;
3906
3907         /* And list them.  */
3908         printf("\nSymbols:\n");
3909         for (p = all; p < all + nsyms; ++p) {
3910                 char type = '?';
3911                 unsigned long value;
3912
3913                 sym = *p;
3914                 if (sym->secidx == SHN_ABS) {
3915                         type = 'A';
3916                         value = sym->value;
3917                 } else if (sym->secidx == SHN_UNDEF) {
3918                         type = 'U';
3919                         value = 0;
3920                 } else {
3921                         sec = f->sections[sym->secidx];
3922
3923                         if (sec->header.sh_type == SHT_NOBITS)
3924                                 type = 'B';
3925                         else if (sec->header.sh_flags & SHF_ALLOC) {
3926                                 if (sec->header.sh_flags & SHF_EXECINSTR)
3927                                         type = 'T';
3928                                 else if (sec->header.sh_flags & SHF_WRITE)
3929                                         type = 'D';
3930                                 else
3931                                         type = 'R';
3932                         }
3933                         value = sym->value + sec->header.sh_addr;
3934                 }
3935
3936                 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3937                         type = tolower(type);
3938
3939                 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3940                                 type, sym->name);
3941         }
3942 #endif
3943 }
3944 #else /* !FEATURE_INSMOD_LOAD_MAP */
3945 void print_load_map(struct obj_file *f);
3946 #endif
3947
3948 int insmod_main( int argc, char **argv);
3949 int insmod_main( int argc, char **argv)
3950 {
3951         char *opt_o, *arg1;
3952         int len;
3953         int k_crcs;
3954         char *tmp, *tmp1;
3955         unsigned long m_size;
3956         ElfW(Addr) m_addr;
3957         struct obj_file *f;
3958         struct stat st;
3959         char *m_name = 0;
3960         int exit_status = EXIT_FAILURE;
3961         int m_has_modinfo;
3962 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3963         struct utsname uts_info;
3964         char m_strversion[STRVERSIONLEN];
3965         int m_version, m_crcs;
3966 #endif
3967 #if ENABLE_FEATURE_CLEAN_UP
3968         FILE *fp = 0;
3969 #else
3970         FILE *fp;
3971 #endif
3972         int k_version = 0;
3973         struct utsname myuname;
3974
3975         /* Parse any options */
3976         getopt32(argc, argv, OPTION_STR, &opt_o);
3977         arg1 = argv[optind];
3978         if (option_mask32 & OPT_o) { // -o /* name the output module */
3979                 free(m_name);
3980                 m_name = xstrdup(opt_o);
3981         }
3982
3983         if (arg1 == NULL) {
3984                 bb_show_usage();
3985         }
3986
3987         /* Grab the module name */
3988         tmp1 = xstrdup(arg1);
3989         tmp = basename(tmp1);
3990         len = strlen(tmp);
3991
3992         if (uname(&myuname) == 0) {
3993                 if (myuname.release[0] == '2') {
3994                         k_version = myuname.release[2] - '0';
3995                 }
3996         }
3997
3998 #if ENABLE_FEATURE_2_6_MODULES
3999         if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
4000          && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
4001         ) {
4002                 len -= 3;
4003                 tmp[len] = '\0';
4004         } else
4005 #endif
4006                 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4007                         len -= 2;
4008                         tmp[len] = '\0';
4009                 }
4010
4011
4012 #if ENABLE_FEATURE_2_6_MODULES
4013         if (k_version > 4)
4014                 m_fullName = xasprintf("%s.ko", tmp);
4015         else
4016 #endif
4017                 m_fullName = xasprintf("%s.o", tmp);
4018
4019         if (!m_name) {
4020                 m_name = tmp;
4021         } else {
4022                 free(tmp1);
4023                 tmp1 = 0;       /* flag for free(m_name) before exit() */
4024         }
4025
4026         /* Get a filedesc for the module.  Check we we have a complete path */
4027         if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
4028          || (fp = fopen(arg1, "r")) == NULL
4029         ) {
4030                 /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
4031                  * but do not error out yet if we fail to find it... */
4032                 if (k_version) {        /* uname succeedd */
4033                         char *module_dir;
4034                         char *tmdn;
4035                         char real_module_dir[FILENAME_MAX];
4036
4037                         tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4038                         /* Jump through hoops in case /lib/modules/`uname -r`
4039                          * is a symlink.  We do not want recursive_action to
4040                          * follow symlinks, but we do want to follow the
4041                          * /lib/modules/`uname -r` dir, So resolve it ourselves
4042                          * if it is a link... */
4043                         if (realpath(tmdn, real_module_dir) == NULL)
4044                                 module_dir = tmdn;
4045                         else
4046                                 module_dir = real_module_dir;
4047                         recursive_action(module_dir, action_recurse,
4048                                         check_module_name_match, 0, m_fullName, 0);
4049                         free(tmdn);
4050                 }
4051
4052                 /* Check if we have found anything yet */
4053                 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
4054                         char module_dir[FILENAME_MAX];
4055
4056                         free(m_filename);
4057                         m_filename = 0;
4058                         if (realpath (_PATH_MODULES, module_dir) == NULL)
4059                                 strcpy(module_dir, _PATH_MODULES);
4060                         /* No module found under /lib/modules/`uname -r`, this
4061                          * time cast the net a bit wider.  Search /lib/modules/ */
4062                         if (!recursive_action(module_dir, action_recurse,
4063                                                     check_module_name_match, 0, m_fullName, 0)
4064                         ) {
4065                                 if (m_filename == 0
4066                                  || ((fp = fopen(m_filename, "r")) == NULL)
4067                                 ) {
4068                                         bb_error_msg("%s: no module by that name found", m_fullName);
4069                                         goto out;
4070                                 }
4071                         } else
4072                                 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4073                 }
4074         } else
4075                 m_filename = xstrdup(arg1);
4076
4077         if (flag_verbose)
4078                 printf("Using %s\n", m_filename);
4079
4080 #if ENABLE_FEATURE_2_6_MODULES
4081         if (k_version > 4) {
4082                 argv[optind] = m_filename;
4083                 optind--;
4084                 return insmod_ng_main(argc - optind, argv + optind);
4085         }
4086 #endif
4087
4088         f = obj_load(fp, LOADBITS);
4089         if (f == NULL)
4090                 bb_perror_msg_and_die("cannot load the module");
4091
4092         if (get_modinfo_value(f, "kernel_version") == NULL)
4093                 m_has_modinfo = 0;
4094         else
4095                 m_has_modinfo = 1;
4096
4097 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4098         /* Version correspondence?  */
4099         if (!flag_quiet) {
4100                 if (uname(&uts_info) < 0)
4101                         uts_info.release[0] = '\0';
4102                 if (m_has_modinfo) {
4103                         m_version = new_get_module_version(f, m_strversion);
4104                         if (m_version == -1) {
4105                                 bb_error_msg("cannot find the kernel version the module was "
4106                                                 "compiled for");
4107                                 goto out;
4108                         }
4109                 }
4110
4111                 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4112                         if (flag_force_load) {
4113                                 bb_error_msg("warning: kernel-module version mismatch\n"
4114                                                 "\t%s was compiled for kernel version %s\n"
4115                                                 "\twhile this kernel is version %s",
4116                                                 m_filename, m_strversion, uts_info.release);
4117                         } else {
4118                                 bb_error_msg("kernel-module version mismatch\n"
4119                                                 "\t%s was compiled for kernel version %s\n"
4120                                                 "\twhile this kernel is version %s.",
4121                                                 m_filename, m_strversion, uts_info.release);
4122                                 goto out;
4123                         }
4124                 }
4125         }
4126         k_crcs = 0;
4127 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4128
4129         if (!query_module(NULL, 0, NULL, 0, NULL)) {
4130                 if (!new_get_kernel_symbols())
4131                         goto out;
4132                 k_crcs = new_is_kernel_checksummed();
4133         } else {
4134                 bb_error_msg("not configured to support old kernels");
4135                 goto out;
4136         }
4137
4138 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4139         m_crcs = 0;
4140         if (m_has_modinfo)
4141                 m_crcs = new_is_module_checksummed(f);
4142
4143         if (m_crcs != k_crcs)
4144                 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4145 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4146
4147         /* Let the module know about the kernel symbols.  */
4148         add_kernel_symbols(f);
4149
4150         /* Allocate common symbols, symbol tables, and string tables.  */
4151
4152         if (!new_create_this_module(f, m_name)) {
4153                 goto out;
4154         }
4155
4156         if (!obj_check_undefineds(f)) {
4157                 goto out;
4158         }
4159         obj_allocate_commons(f);
4160         check_tainted_module(f, m_name);
4161
4162         /* done with the module name, on to the optional var=value arguments */
4163         ++optind;
4164         if (optind < argc) {
4165                 if (!new_process_module_arguments(f, argc - optind, argv + optind)) {
4166                         goto out;
4167                 }
4168         }
4169
4170         arch_create_got(f);
4171         hide_special_symbols(f);
4172
4173 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4174         add_ksymoops_symbols(f, m_filename, m_name);
4175 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4176
4177         new_create_module_ksymtab(f);
4178
4179         /* Find current size of the module */
4180         m_size = obj_load_size(f);
4181
4182
4183         m_addr = create_module(m_name, m_size);
4184         if (m_addr == -1) switch (errno) {
4185                 case EEXIST:
4186                         bb_error_msg("a module named %s already exists", m_name);
4187                         goto out;
4188                 case ENOMEM:
4189                         bb_error_msg("can't allocate kernel memory for module; needed %lu bytes",
4190                                         m_size);
4191                         goto out;
4192                 default:
4193                         bb_perror_msg("create_module: %s", m_name);
4194                         goto out;
4195         }
4196
4197 #if  !LOADBITS
4198         /*
4199          * the PROGBITS section was not loaded by the obj_load
4200          * now we can load them directly into the kernel memory
4201          */
4202         if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4203                 delete_module(m_name);
4204                 goto out;
4205         }
4206 #endif
4207
4208         if (!obj_relocate(f, m_addr)) {
4209                 delete_module(m_name);
4210                 goto out;
4211         }
4212
4213         if (!new_init_module(m_name, f, m_size)) {
4214                 delete_module(m_name);
4215                 goto out;
4216         }
4217
4218         if(flag_print_load_map)
4219                 print_load_map(f);
4220
4221         exit_status = EXIT_SUCCESS;
4222
4223 out:
4224 #if ENABLE_FEATURE_CLEAN_UP
4225         if(fp)
4226                 fclose(fp);
4227         free(tmp1);
4228         if(!tmp1)
4229                 free(m_name);
4230         free(m_filename);
4231 #endif
4232         return exit_status;
4233 }
4234
4235
4236 #endif
4237
4238
4239 #if ENABLE_FEATURE_2_6_MODULES
4240
4241 #include <sys/mman.h>
4242 #include <asm/unistd.h>
4243 #include <sys/syscall.h>
4244
4245 /* We use error numbers in a loose translation... */
4246 static const char *moderror(int err)
4247 {
4248         switch (err) {
4249         case ENOEXEC:
4250                 return "Invalid module format";
4251         case ENOENT:
4252                 return "Unknown symbol in module";
4253         case ESRCH:
4254                 return "Module has wrong symbol version";
4255         case EINVAL:
4256                 return "Invalid parameters";
4257         default:
4258                 return strerror(err);
4259         }
4260 }
4261
4262 int insmod_ng_main(int argc, char **argv);
4263 int insmod_ng_main(int argc, char **argv)
4264 {
4265         long ret;
4266         size_t len;
4267         void *map;
4268         char *filename, *options;
4269
4270         filename = *++argv;
4271         if (!filename)
4272                 bb_show_usage();
4273
4274         /* Rest is options */
4275         options = xstrdup("");
4276         while (*++argv) {
4277                 int optlen = strlen(options);
4278                 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4279                 /* Spaces handled by "" pairs, but no way of escaping quotes */
4280                 sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4281         }
4282
4283 #if 0
4284         /* Any special reason why mmap? It isn't performace critical... */
4285         int fd;
4286         struct stat st;
4287         unsigned long len;
4288         fd = xopen(filename, O_RDONLY);
4289         fstat(fd, &st);
4290         len = st.st_size;
4291         map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4292         if (map == MAP_FAILED) {
4293                 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4294         }
4295
4296         /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4297         if (map == NULL) {
4298                 map = xmalloc(len);
4299                 xread(fd, map, len);
4300         }
4301 #else
4302         len = MAXINT(ssize_t);
4303         map = xmalloc_open_read_close(filename, &len);
4304 #endif
4305
4306         ret = syscall(__NR_init_module, map, len, options);
4307         if (ret != 0) {
4308                 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
4309                                 filename, moderror(errno), ret);
4310         }
4311
4312         return 0;
4313 }
4314
4315 #endif