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