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