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