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