1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
#if defined(ELF32BIT)
#define PERBIT(x) x##32
#define ElfPERBIT(x) Elf32_##x
#define ELFPERBIT(x) ELF32_##x
/* 32-bit unsigned integer */
#define Elf32_Uint Elf32_Word
#elif defined(ELF64BIT)
#define PERBIT(x) x##64
#define ElfPERBIT(x) Elf64_##x
#define ELFPERBIT(x) ELF64_##x
/* 64-bit unsigned integer */
#define Elf64_Uint Elf64_Xword
#else
# error "Undefined ELF word length"
#endif
static void *PERBIT(get_section)(struct elf_file *module,
const char *secname,
ElfPERBIT(Shdr) **sechdr,
unsigned long *secsize)
{
void *data = module->data;
unsigned long len = module->len;
int conv = module->conv;
ElfPERBIT(Ehdr) *hdr;
ElfPERBIT(Shdr) *sechdrs;
ElfPERBIT(Off) e_shoff;
ElfPERBIT(Half) e_shnum, e_shstrndx;
ElfPERBIT(Off) secoffset;
const char *secnames;
unsigned int i;
*secsize = 0;
if (len <= 0 || len < sizeof(*hdr))
return NULL;
hdr = data;
e_shoff = END(hdr->e_shoff, conv);
e_shnum = END(hdr->e_shnum, conv);
e_shstrndx = END(hdr->e_shstrndx, conv);
if (len < e_shoff + e_shnum * sizeof(sechdrs[0]))
return NULL;
sechdrs = data + e_shoff;
if (len < END(sechdrs[e_shstrndx].sh_offset, conv))
return NULL;
/* Find section by name; return header, pointer and size. */
secnames = data + END(sechdrs[e_shstrndx].sh_offset, conv);
for (i = 1; i < e_shnum; i++) {
if (streq(secnames + END(sechdrs[i].sh_name, conv), secname)) {
*secsize = END(sechdrs[i].sh_size, conv);
secoffset = END(sechdrs[i].sh_offset, conv);
if (sechdr)
*sechdr = sechdrs + i;
if (len < secoffset + *secsize)
return NULL;
return data + secoffset;
}
}
return NULL;
}
/* Load the given section: NULL on error. */
static void *PERBIT(load_section)(struct elf_file *module,
const char *secname,
unsigned long *secsize)
{
return PERBIT(get_section)(module, secname, NULL, secsize);
}
static struct string_table *PERBIT(load_strings)(struct elf_file *module,
const char *secname,
struct string_table *tbl)
{
unsigned long size;
const char *strings;
strings = PERBIT(load_section)(module, secname, &size);
if (strings) {
if (strings[size-1] != 0) {
warn("%s may be corrupt; an unterminated string"
" was found at the end of section %s\n",
module->pathname, secname);
}
/* Skip any zero padding. */
while (!strings[0]) {
strings++;
if (size-- <= 1)
return tbl;
}
for (; strings; strings = next_string(strings, &size))
tbl = NOFAIL(strtbl_add(strings, tbl));
}
return tbl;
}
static struct string_table *PERBIT(load_symbols)(struct elf_file *module,
uint64_t **versions)
{
struct string_table *symtbl = NULL;
if (versions) {
static const char crc[] = "__crc_";
static const int crc_len = sizeof(crc) - 1;
unsigned int num_syms, i;
unsigned long size;
ElfPERBIT(Sym) *syms;
char *strings;
int conv;
*versions = NULL;
strings = PERBIT(load_section)(module, ".strtab", &size);
syms = PERBIT(load_section)(module, ".symtab", &size);
if (!strings || !syms)
goto fallback;
num_syms = size / sizeof(syms[0]);
*versions = NOFAIL(calloc(sizeof(**versions), num_syms));
conv = module->conv;
for (i = 1; i < num_syms; i++) {
const char *name;
name = strings + END(syms[i].st_name, conv);
if (strncmp(name, crc, crc_len) != 0)
continue;
name += crc_len;
symtbl = NOFAIL(strtbl_add(name, symtbl));
(*versions)[symtbl->cnt - 1] = END(syms[i].st_value,
conv);
}
if (!symtbl) {
/* Either this module does not export any symbols, or
* it was compiled without CONFIG_MODVERSIONS. If the
* latter, we will print a warning in load_dep_syms,
* so just silently fallback to __ksymtab_strings in
* both cases.
*/
free(*versions);
*versions = NULL;
goto fallback;
}
return symtbl;
}
fallback:
return PERBIT(load_strings)(module, "__ksymtab_strings", symtbl);
}
static char *PERBIT(get_aliases)(struct elf_file *module, unsigned long *size)
{
return PERBIT(load_section)(module, ".modalias", size);
}
static char *PERBIT(get_modinfo)(struct elf_file *module, unsigned long *size)
{
return PERBIT(load_section)(module, ".modinfo", size);
}
#ifndef STT_REGISTER
#define STT_REGISTER 13 /* Global register reserved to app. */
#endif
static struct string_table *PERBIT(load_dep_syms)(struct elf_file *module,
struct string_table **types,
uint64_t **versions)
{
unsigned int i, num_syms;
unsigned int j, num_symvers, versions_size;
unsigned long size;
char *strings;
ElfPERBIT(Sym) *syms;
ElfPERBIT(Ehdr) *hdr;
struct PERBIT(modver_info) **symvers;
int handle_register_symbols;
struct string_table *names;
int conv;
names = NULL;
*types = NULL;
symvers = NULL;
num_symvers = versions_size = 0;
if (versions) {
int ok = 1;
*versions = NULL;
struct PERBIT(modver_info) *symvers_sec;
symvers_sec = module->ops->load_section(module, "__versions",
&size);
if (!symvers_sec) {
warn("%s is built without modversions",
module->pathname);
ok = 0;
}
if (size % sizeof(symvers[0]) != 0) {
warn("invalid __versions section size in %s",
module->pathname);
ok = 0;
}
if (ok) {
num_symvers = size / sizeof(symvers_sec[0]);
/* symvers is used to keep track of each visited entry.
* The table also contains the fake struct_module /
* module_layout symbol which we don't want to miss.
*/
symvers = NOFAIL(malloc(num_symvers *
sizeof(symvers[0])));
for (j = 0; j < num_symvers; j++)
symvers[j] = &symvers_sec[j];
} else {
versions = NULL;
}
}
strings = PERBIT(load_section)(module, ".strtab", &size);
syms = PERBIT(load_section)(module, ".symtab", &size);
if (!strings || !syms) {
warn("Couldn't find symtab and strtab in module %s\n",
module->pathname);
goto out;
}
num_syms = size / sizeof(syms[0]);
hdr = module->data;
conv = module->conv;
if (versions) {
versions_size = num_syms;
*versions = NOFAIL(calloc(sizeof(**versions), versions_size));
}
handle_register_symbols =
(END(hdr->e_machine, conv) == EM_SPARC ||
END(hdr->e_machine, conv) == EM_SPARCV9);
for (i = 1; i < num_syms; i++) {
if (END(syms[i].st_shndx, conv) == SHN_UNDEF) {
/* Look for symbol */
const char *name;
int weak;
name = strings + END(syms[i].st_name, conv);
/* Not really undefined: sparc gcc 3.3 creates
U references when you have global asm
variables, to avoid anyone else misusing
them. */
if (handle_register_symbols
&& (ELFPERBIT(ST_TYPE)(END(syms[i].st_info, conv))
== STT_REGISTER))
continue;
weak = (ELFPERBIT(ST_BIND)(END(syms[i].st_info, conv))
== STB_WEAK);
names = NOFAIL(strtbl_add(name, names));
*types = NOFAIL(strtbl_add(weak ? weak_sym : undef_sym,
*types));
if (!versions)
continue;
/* Not optimal, but the number of required symbols
* is usually not huge and this is only called by
* depmod.
*/
for (j = 0; j < num_symvers; j++) {
struct PERBIT(modver_info) *info = symvers[j];
if (!info)
continue;
if (streq(name, info->name)) {
(*versions)[names->cnt - 1] =
END(info->crc, conv);
symvers[j] = NULL;
break;
}
}
}
}
/* add struct_module / module_layout */
for (j = 0; j < num_symvers; j++) {
struct PERBIT(modver_info) *info = symvers[j];
if (!info)
continue;
if ((names ? names->cnt : 0) >= versions_size) {
versions_size++;
*versions = NOFAIL(realloc(*versions, versions_size));
}
names = NOFAIL(strtbl_add(info->name, names));
*types = NOFAIL(strtbl_add(undef_sym, *types));
(*versions)[names->cnt - 1] = END(info->crc, conv);
}
out:
free(symvers);
return names;
}
static void *PERBIT(deref_sym)(ElfPERBIT(Ehdr) *hdr,
ElfPERBIT(Shdr) *sechdrs,
ElfPERBIT(Sym) *sym,
unsigned int *secsize,
int conv)
{
/* In BSS? Happens for empty device tables on
* recent GCC versions. */
if (END(sechdrs[END(sym->st_shndx, conv)].sh_type,conv) == SHT_NOBITS)
return NULL;
if (secsize)
*secsize = END(sym->st_size, conv);
return (void *)hdr
+ END(sechdrs[END(sym->st_shndx, conv)].sh_offset, conv)
+ END(sym->st_value, conv);
}
/* FIXME: Check size, unless we end up using aliases anyway --RR */
static void PERBIT(fetch_tables)(struct elf_file *module,
struct module_tables *tables)
{
unsigned int i;
unsigned long size;
char *strings;
ElfPERBIT(Ehdr) *hdr;
ElfPERBIT(Sym) *syms;
ElfPERBIT(Shdr) *sechdrs;
int conv;
hdr = module->data;
conv = module->conv;
sechdrs = (void *)hdr + END(hdr->e_shoff, conv);
strings = PERBIT(load_section)(module, ".strtab", &size);
syms = PERBIT(load_section)(module, ".symtab", &size);
/* Don't warn again: we already have above */
if (!strings || !syms)
return;
memset(tables, 0x00, sizeof(struct module_tables));
for (i = 0; i < size / sizeof(syms[0]); i++) {
char *name = strings + END(syms[i].st_name, conv);
if (!tables->pci_table && streq(name, "__mod_pci_device_table")) {
tables->pci_size = PERBIT(PCI_DEVICE_SIZE);
tables->pci_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
else if (!tables->usb_table && streq(name, "__mod_usb_device_table")) {
tables->usb_size = PERBIT(USB_DEVICE_SIZE);
tables->usb_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
else if (!tables->ccw_table && streq(name, "__mod_ccw_device_table")) {
tables->ccw_size = PERBIT(CCW_DEVICE_SIZE);
tables->ccw_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
else if (!tables->ieee1394_table && streq(name, "__mod_ieee1394_device_table")) {
tables->ieee1394_size = PERBIT(IEEE1394_DEVICE_SIZE);
tables->ieee1394_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
else if (!tables->pnp_table && streq(name, "__mod_pnp_device_table")) {
tables->pnp_size = PERBIT(PNP_DEVICE_SIZE);
tables->pnp_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
else if (!tables->pnp_card_table && streq(name, "__mod_pnp_card_device_table")) {
tables->pnp_card_size = PERBIT(PNP_CARD_DEVICE_SIZE);
tables->pnp_card_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
tables->pnp_card_offset = PERBIT(PNP_CARD_DEVICE_OFFSET);
}
else if (!tables->input_table && streq(name, "__mod_input_device_table")) {
tables->input_size = PERBIT(INPUT_DEVICE_SIZE);
tables->input_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
&tables->input_table_size,
conv);
}
else if (!tables->serio_table && streq(name, "__mod_serio_device_table")) {
tables->serio_size = PERBIT(SERIO_DEVICE_SIZE);
tables->serio_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
else if (!tables->of_table && streq(name, "__mod_of_device_table")) {
tables->of_size = PERBIT(OF_DEVICE_SIZE);
tables->of_table = PERBIT(deref_sym)(hdr, sechdrs, &syms[i],
NULL, conv);
}
}
}
/*
* strip_section - tell the kernel to ignore the named section
*/
static void PERBIT(strip_section)(struct elf_file *module, const char *secname)
{
void *p;
ElfPERBIT(Shdr) *sechdr;
unsigned long secsize;
p = PERBIT(get_section)(module, secname, &sechdr, &secsize);
if (p) {
ElfPERBIT(Uint) mask;
mask = ~((ElfPERBIT(Uint))SHF_ALLOC);
sechdr->sh_flags &= END(mask, module->conv);
}
}
static int PERBIT(dump_modversions)(struct elf_file *module)
{
unsigned long secsize;
struct PERBIT(modver_info) *info;
int n = 0;
info = module->ops->load_section(module, "__versions", &secsize);
if (!info)
return 0; /* not a kernel module */
if (secsize % sizeof(*info) != 0)
return -1; /* invalid section size */
for (n = 0; n < secsize / sizeof(*info); n++) {
#if defined(ELF32BIT)
printf("0x%08lx\t%s\n", (unsigned long)
#else /* defined(ELF64BIT) */
printf("0x%08llx\t%s\n", (unsigned long long)
#endif
END(info[n].crc, module->conv),
skip_dot(info[n].name));
}
return n;
}
struct module_ops PERBIT(mod_ops) = {
.load_section = PERBIT(load_section),
.load_strings = PERBIT(load_strings),
.load_symbols = PERBIT(load_symbols),
.load_dep_syms = PERBIT(load_dep_syms),
.fetch_tables = PERBIT(fetch_tables),
.get_aliases = PERBIT(get_aliases),
.get_modinfo = PERBIT(get_modinfo),
.strip_section = PERBIT(strip_section),
.dump_modvers = PERBIT(dump_modversions),
};
#undef PERBIT
#undef ElfPERBIT
#undef ELFPERBIT
|