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