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