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