2014-05-14 20:43:58 +00:00
|
|
|
/*
|
|
|
|
* lookup.c
|
|
|
|
*
|
|
|
|
* This file contains functions that assist in the reading and searching
|
|
|
|
* the symbol table of an ELF object.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
|
2017-01-23 20:43:12 +00:00
|
|
|
* Copyright (C) 2014 Josh Poimboeuf <jpoimboe@redhat.com>
|
2014-05-14 20:43:58 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-01-23 20:43:12 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <ctype.h>
|
2014-05-14 20:43:58 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2022-08-05 06:52:03 +00:00
|
|
|
#include <err.h>
|
2014-05-14 20:43:58 +00:00
|
|
|
#include <gelf.h>
|
|
|
|
#include <unistd.h>
|
2017-01-23 20:43:12 +00:00
|
|
|
#include <libgen.h>
|
2020-04-06 20:05:06 +00:00
|
|
|
#include <stdbool.h>
|
2014-05-14 20:43:58 +00:00
|
|
|
|
|
|
|
#include "lookup.h"
|
2017-01-25 16:02:58 +00:00
|
|
|
#include "log.h"
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
struct object_symbol {
|
2018-04-18 18:39:56 +00:00
|
|
|
unsigned long addr;
|
2014-05-14 20:43:58 +00:00
|
|
|
unsigned long size;
|
|
|
|
char *name;
|
2018-05-11 09:51:00 +00:00
|
|
|
int type, bind;
|
2014-05-14 20:43:58 +00:00
|
|
|
};
|
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
struct export_symbol {
|
|
|
|
char *name;
|
|
|
|
char *objname;
|
|
|
|
};
|
|
|
|
|
2014-05-14 20:43:58 +00:00
|
|
|
struct lookup_table {
|
2017-01-23 20:43:12 +00:00
|
|
|
int obj_nr, exp_nr;
|
|
|
|
struct object_symbol *obj_syms;
|
|
|
|
struct export_symbol *exp_syms;
|
2019-11-01 20:03:14 +00:00
|
|
|
char *objname;
|
2014-05-14 20:43:58 +00:00
|
|
|
};
|
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
#define for_each_obj_symbol(ndx, iter, table) \
|
|
|
|
for (ndx = 0, iter = table->obj_syms; ndx < table->obj_nr; ndx++, iter++)
|
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
#define for_each_obj_symbol_continue(ndx, iter, table) \
|
|
|
|
for (iter = table->obj_syms + ndx; ndx < table->obj_nr; ndx++, iter++)
|
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
#define for_each_exp_symbol(ndx, iter, table) \
|
|
|
|
for (ndx = 0, iter = table->exp_syms; ndx < table->exp_nr; ndx++, iter++)
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2021-08-11 08:33:44 +00:00
|
|
|
static bool maybe_discarded_sym(const char *name)
|
2017-03-03 14:27:28 +00:00
|
|
|
{
|
2018-03-21 17:47:58 +00:00
|
|
|
if (!name)
|
2021-08-11 08:33:44 +00:00
|
|
|
return false;
|
2018-03-21 17:47:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sometimes these symbols are discarded during linking, and sometimes
|
|
|
|
* they're not, depending on whether the parent object is vmlinux or a
|
|
|
|
* module, and also depending on the kernel version. For simplicity,
|
|
|
|
* we just always skip them when comparing object symbol tables.
|
|
|
|
*/
|
|
|
|
if (!strncmp(name, "__exitcall_", 11) ||
|
|
|
|
!strncmp(name, "__brk_reservation_fn_", 21) ||
|
2018-10-16 14:05:01 +00:00
|
|
|
!strncmp(name, "__func_stack_frame_non_standard_", 32) ||
|
2021-02-03 11:40:34 +00:00
|
|
|
strstr(name, "__addressable_") ||
|
|
|
|
strstr(name, "__UNIQUE_ID_") ||
|
2021-02-01 11:05:20 +00:00
|
|
|
!strncmp(name, ".L.str", 6))
|
2021-08-11 08:33:44 +00:00
|
|
|
return true;
|
2017-03-03 14:27:28 +00:00
|
|
|
|
2021-08-11 08:33:44 +00:00
|
|
|
return false;
|
2017-03-03 14:27:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 08:33:44 +00:00
|
|
|
static bool locals_match(struct lookup_table *table, int idx,
|
2021-08-02 09:32:23 +00:00
|
|
|
struct symbol *file_sym, struct list_head *sym_list)
|
2017-02-04 01:31:25 +00:00
|
|
|
{
|
2021-08-02 09:32:23 +00:00
|
|
|
struct symbol *sym;
|
|
|
|
struct object_symbol *table_sym;
|
2017-07-06 17:44:50 +00:00
|
|
|
int i, found;
|
|
|
|
|
|
|
|
i = idx + 1;
|
2021-08-02 09:32:23 +00:00
|
|
|
for_each_obj_symbol_continue(i, table_sym, table) {
|
|
|
|
if (table_sym->type == STT_FILE)
|
2017-07-06 17:44:50 +00:00
|
|
|
break;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (table_sym->bind != STB_LOCAL)
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (table_sym->type != STT_FUNC && table_sym->type != STT_OBJECT)
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
found = 0;
|
2021-08-02 09:32:23 +00:00
|
|
|
sym = file_sym;
|
|
|
|
list_for_each_entry_continue(sym, sym_list, list) {
|
|
|
|
if (sym->type == STT_FILE)
|
|
|
|
break;
|
|
|
|
if (sym->bind != STB_LOCAL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sym->type == table_sym->type &&
|
|
|
|
!strcmp(sym->name, table_sym->name)) {
|
2017-07-06 17:44:50 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
if (!found)
|
2021-08-11 08:33:44 +00:00
|
|
|
return false;
|
2017-07-06 17:44:50 +00:00
|
|
|
}
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2021-08-02 09:32:23 +00:00
|
|
|
sym = file_sym;
|
|
|
|
list_for_each_entry_continue(sym, sym_list, list) {
|
|
|
|
if (sym->type == STT_FILE)
|
|
|
|
break;
|
|
|
|
if (sym->bind != STB_LOCAL)
|
|
|
|
continue;
|
2022-02-03 10:59:24 +00:00
|
|
|
if (sym->type != STT_FUNC && sym->type != STT_OBJECT)
|
2021-08-02 09:32:23 +00:00
|
|
|
continue;
|
2017-03-03 14:27:28 +00:00
|
|
|
/*
|
|
|
|
* Symbols which get discarded at link time are missing from
|
|
|
|
* the lookup table, so skip them.
|
|
|
|
*/
|
2021-08-02 09:32:23 +00:00
|
|
|
if (maybe_discarded_sym(sym->name))
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
i = idx + 1;
|
2021-08-02 09:32:23 +00:00
|
|
|
for_each_obj_symbol_continue(i, table_sym, table) {
|
|
|
|
if (table_sym->type == STT_FILE)
|
2017-07-06 17:44:50 +00:00
|
|
|
break;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (table_sym->bind != STB_LOCAL)
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (maybe_discarded_sym(table_sym->name))
|
2018-03-21 17:47:58 +00:00
|
|
|
continue;
|
2017-07-06 17:44:50 +00:00
|
|
|
|
2021-08-02 09:32:23 +00:00
|
|
|
if (sym->type == table_sym->type &&
|
|
|
|
!strcmp(sym->name, table_sym->name)) {
|
2017-07-06 17:44:50 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
2021-08-11 08:33:44 +00:00
|
|
|
return false;
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 08:33:44 +00:00
|
|
|
return true;
|
2017-07-06 17:44:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 09:32:23 +00:00
|
|
|
static void find_local_syms(struct lookup_table *table, struct symbol *file_sym,
|
|
|
|
struct list_head *sym_list)
|
2017-07-06 17:44:50 +00:00
|
|
|
{
|
|
|
|
struct object_symbol *sym;
|
2021-08-02 09:32:23 +00:00
|
|
|
struct object_symbol *lookup_table_file_sym = NULL;
|
2017-07-06 17:44:50 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_obj_symbol(i, sym, table) {
|
|
|
|
if (sym->type != STT_FILE)
|
|
|
|
continue;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (strcmp(file_sym->name, sym->name))
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (!locals_match(table, i, file_sym, sym_list))
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
2021-08-02 09:32:23 +00:00
|
|
|
if (lookup_table_file_sym)
|
2019-11-01 20:03:14 +00:00
|
|
|
ERROR("found duplicate matches for %s local symbols in %s symbol table",
|
2021-08-02 09:32:23 +00:00
|
|
|
file_sym->name, table->objname);
|
2017-07-06 17:44:50 +00:00
|
|
|
|
2021-08-02 09:32:23 +00:00
|
|
|
lookup_table_file_sym = sym;
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 09:32:23 +00:00
|
|
|
if (!lookup_table_file_sym)
|
2019-11-01 20:03:14 +00:00
|
|
|
ERROR("couldn't find matching %s local symbols in %s symbol table",
|
2021-08-02 09:32:23 +00:00
|
|
|
file_sym->name, table->objname);
|
|
|
|
|
|
|
|
list_for_each_entry_continue(file_sym, sym_list, list) {
|
|
|
|
if (file_sym->type == STT_FILE)
|
|
|
|
break;
|
|
|
|
file_sym->lookup_table_file_sym = lookup_table_file_sym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because there can be duplicate symbols and duplicate filenames we need to
|
|
|
|
* correlate each symbol from the elf file to it's corresponding symbol in
|
|
|
|
* lookup table. Both the elf file and the lookup table can be split on
|
|
|
|
* STT_FILE symbols into blocks of symbols originating from a single source
|
|
|
|
* file. We then compare local symbol lists from both blocks and store the
|
|
|
|
* pointer to STT_FILE symbol in lookup table for later use in
|
|
|
|
* lookup_local_symbol().
|
|
|
|
*/
|
|
|
|
static void find_local_syms_multiple(struct lookup_table *table,
|
|
|
|
struct kpatch_elf *kelf)
|
|
|
|
{
|
|
|
|
struct symbol *sym;
|
|
|
|
|
|
|
|
list_for_each_entry(sym, &kelf->symbols, list) {
|
|
|
|
if (sym->type == STT_FILE)
|
|
|
|
find_local_syms(table, sym, &kelf->symbols);
|
|
|
|
}
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
/* Strip the path and replace '-' with '_' */
|
|
|
|
static char *make_modname(char *modname)
|
2014-05-14 20:43:58 +00:00
|
|
|
{
|
2019-04-16 01:21:12 +00:00
|
|
|
char *cur, *name;
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
if (!modname)
|
|
|
|
return NULL;
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2019-04-16 01:21:12 +00:00
|
|
|
name = strdup(basename(modname));
|
|
|
|
if (!name)
|
2019-07-31 12:53:54 +00:00
|
|
|
ERROR("strdup");
|
2019-04-16 01:21:12 +00:00
|
|
|
|
|
|
|
cur = name; /* use cur as tmp */
|
2018-05-11 09:51:00 +00:00
|
|
|
while (*cur != '\0') {
|
|
|
|
if (*cur == '-')
|
|
|
|
*cur = '_';
|
|
|
|
cur++;
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 01:21:12 +00:00
|
|
|
return name;
|
2018-05-11 09:51:00 +00:00
|
|
|
}
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
static void symtab_read(struct lookup_table *table, char *path)
|
|
|
|
{
|
|
|
|
FILE *file;
|
2018-04-18 18:39:56 +00:00
|
|
|
long unsigned int addr;
|
2020-04-06 20:01:26 +00:00
|
|
|
int alloc_nr = 0, i = 0;
|
2018-09-12 14:23:17 +00:00
|
|
|
int matched;
|
2020-04-06 20:05:06 +00:00
|
|
|
bool skip = false;
|
kpatch-build: use readelf instead of eu-readelf
readelf is more standard, using readelf insteaded we should solve there
issues:
First, using "readelf -s", the symbol name would truncated by 25 chars,
to solve this issue, add option "--wide".
Second, the size may be mixed of decimal and hex, we get the size by "%s",
and use strtoul(size, NULL, 0) to convert the size.
Third, the symbol type is SHN_UNDE, the Ndx display "UND", so changed to
compare with "UND".
Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-28 12:21:22 +00:00
|
|
|
char line[256], name[256], size[16], type[16], bind[16], ndx[16];
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
if ((file = fopen(path, "r")) == NULL)
|
|
|
|
ERROR("fopen");
|
2014-05-15 18:25:27 +00:00
|
|
|
|
2020-04-06 20:01:26 +00:00
|
|
|
/*
|
|
|
|
* First, get an upper limit on the number of entries for allocation
|
|
|
|
* purposes:
|
|
|
|
*/
|
|
|
|
while (fgets(line, 256, file))
|
|
|
|
alloc_nr++;
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2020-04-06 20:01:26 +00:00
|
|
|
table->obj_syms = malloc(alloc_nr * sizeof(*table->obj_syms));
|
2017-01-23 20:43:12 +00:00
|
|
|
if (!table->obj_syms)
|
|
|
|
ERROR("malloc table.obj_syms");
|
2020-04-06 20:01:26 +00:00
|
|
|
memset(table->obj_syms, 0, alloc_nr * sizeof(*table->obj_syms));
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
rewind(file);
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2020-04-06 20:01:26 +00:00
|
|
|
/* Now read the actual entries: */
|
2018-05-11 09:51:00 +00:00
|
|
|
while (fgets(line, 256, file)) {
|
2020-04-06 20:05:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* On powerpc, "readelf -s" shows both .dynsym and .symtab
|
|
|
|
* tables. .dynsym is just a subset of .symtab, so skip it to
|
|
|
|
* avoid duplicates.
|
|
|
|
*/
|
2022-03-29 08:20:08 +00:00
|
|
|
if (!strncmp(line, "Symbol table ", 13)) {
|
|
|
|
if (strstr(line, ".dynsym")) {
|
|
|
|
skip = true;
|
|
|
|
continue;
|
|
|
|
} else if (strstr(line, ".symtab")) {
|
|
|
|
skip = false;
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-06 20:05:06 +00:00
|
|
|
}
|
|
|
|
if (skip)
|
|
|
|
continue;
|
|
|
|
|
2019-06-11 20:07:03 +00:00
|
|
|
matched = sscanf(line, "%*s %lx %s %s %s %*s %s %s\n",
|
2018-04-18 18:39:56 +00:00
|
|
|
&addr, size, type, bind, ndx, name);
|
2018-09-12 14:23:17 +00:00
|
|
|
|
|
|
|
if (matched == 5) {
|
|
|
|
name[0] = '\0';
|
|
|
|
matched++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matched != 6 ||
|
kpatch-build: use readelf instead of eu-readelf
readelf is more standard, using readelf insteaded we should solve there
issues:
First, using "readelf -s", the symbol name would truncated by 25 chars,
to solve this issue, add option "--wide".
Second, the size may be mixed of decimal and hex, we get the size by "%s",
and use strtoul(size, NULL, 0) to convert the size.
Third, the symbol type is SHN_UNDE, the Ndx display "UND", so changed to
compare with "UND".
Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-28 12:21:22 +00:00
|
|
|
!strcmp(ndx, "UND") ||
|
2018-09-12 13:57:26 +00:00
|
|
|
!strcmp(type, "SECTION"))
|
2014-06-02 21:30:47 +00:00
|
|
|
continue;
|
2018-06-02 18:30:51 +00:00
|
|
|
|
2018-04-18 18:39:56 +00:00
|
|
|
table->obj_syms[i].addr = addr;
|
kpatch-build: use readelf instead of eu-readelf
readelf is more standard, using readelf insteaded we should solve there
issues:
First, using "readelf -s", the symbol name would truncated by 25 chars,
to solve this issue, add option "--wide".
Second, the size may be mixed of decimal and hex, we get the size by "%s",
and use strtoul(size, NULL, 0) to convert the size.
Third, the symbol type is SHN_UNDE, the Ndx display "UND", so changed to
compare with "UND".
Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
2019-05-28 12:21:22 +00:00
|
|
|
table->obj_syms[i].size = strtoul(size, NULL, 0);
|
2018-05-11 09:51:00 +00:00
|
|
|
|
|
|
|
if (!strcmp(bind, "LOCAL")) {
|
|
|
|
table->obj_syms[i].bind = STB_LOCAL;
|
|
|
|
} else if (!strcmp(bind, "GLOBAL")) {
|
|
|
|
table->obj_syms[i].bind = STB_GLOBAL;
|
|
|
|
} else if (!strcmp(bind, "WEAK")) {
|
|
|
|
table->obj_syms[i].bind = STB_WEAK;
|
|
|
|
} else {
|
|
|
|
ERROR("unknown symbol bind %s", bind);
|
|
|
|
}
|
2014-06-02 21:30:47 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
if (!strcmp(type, "NOTYPE")) {
|
|
|
|
table->obj_syms[i].type = STT_NOTYPE;
|
|
|
|
} else if (!strcmp(type, "OBJECT")) {
|
|
|
|
table->obj_syms[i].type = STT_OBJECT;
|
|
|
|
} else if (!strcmp(type, "FUNC")) {
|
|
|
|
table->obj_syms[i].type = STT_FUNC;
|
|
|
|
} else if (!strcmp(type, "FILE")) {
|
|
|
|
table->obj_syms[i].type = STT_FILE;
|
|
|
|
} else {
|
|
|
|
ERROR("unknown symbol type %s", type);
|
|
|
|
}
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
table->obj_syms[i].name = strdup(name);
|
|
|
|
if (!table->obj_syms[i].name)
|
2017-01-23 20:43:12 +00:00
|
|
|
ERROR("strdup");
|
2020-04-06 20:01:26 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
i++;
|
2017-01-23 20:43:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:01:26 +00:00
|
|
|
table->obj_nr = i;
|
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
fclose(file);
|
2017-01-23 20:43:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 13:33:01 +00:00
|
|
|
/*
|
2020-04-14 14:57:19 +00:00
|
|
|
* The Module.symvers file format is one of the following, depending on kernel
|
|
|
|
* version:
|
|
|
|
*
|
|
|
|
* <CRC> <Symbol> <Module> <Export Type>
|
2019-10-23 13:33:01 +00:00
|
|
|
* <CRC> <Symbol> <Namespace> <Module> <Export Type>
|
2020-04-14 14:57:19 +00:00
|
|
|
* <CRC> <Symbol> <Module> <Export Type> <Namespace>
|
2019-10-23 13:33:01 +00:00
|
|
|
*
|
2020-04-14 14:57:19 +00:00
|
|
|
* All we care about is Symbol and Module. Since the format is unpredictable,
|
|
|
|
* we have to dynamically determine which column is Module by looking for
|
|
|
|
* "vmlinux".
|
2019-10-23 13:33:01 +00:00
|
|
|
*/
|
2017-01-23 20:43:12 +00:00
|
|
|
static void symvers_read(struct lookup_table *table, char *path)
|
|
|
|
{
|
|
|
|
FILE *file;
|
2020-04-14 14:57:19 +00:00
|
|
|
int i, column, mod_column = 0;
|
2019-10-23 13:33:01 +00:00
|
|
|
char line[4096];
|
2020-04-14 14:57:19 +00:00
|
|
|
char *tmp, *objname, *symname;
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2018-05-11 09:51:00 +00:00
|
|
|
if ((file = fopen(path, "r")) == NULL)
|
2017-01-23 20:43:12 +00:00
|
|
|
ERROR("fopen");
|
|
|
|
|
2020-04-14 14:57:19 +00:00
|
|
|
while (fgets(line, 4096, file)) {
|
2017-01-23 20:43:12 +00:00
|
|
|
table->exp_nr++;
|
|
|
|
|
2020-04-14 14:57:19 +00:00
|
|
|
if (mod_column)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Find the module column */
|
|
|
|
for (column = 1, tmp = line; (tmp = strchr(tmp, '\t')); column++) {
|
|
|
|
tmp++;
|
|
|
|
if (*tmp && !strncmp(tmp, "vmlinux", 7))
|
|
|
|
mod_column = column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table->exp_nr && !mod_column)
|
|
|
|
ERROR("Module.symvers: invalid format");
|
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
table->exp_syms = malloc(table->exp_nr * sizeof(*table->exp_syms));
|
|
|
|
if (!table->exp_syms)
|
|
|
|
ERROR("malloc table.exp_syms");
|
|
|
|
memset(table->exp_syms, 0,
|
|
|
|
table->exp_nr * sizeof(*table->exp_syms));
|
|
|
|
|
|
|
|
rewind(file);
|
2020-04-14 14:57:19 +00:00
|
|
|
for (i = 0; fgets(line, 4096, file); i++) {
|
|
|
|
char *name = NULL, *mod = NULL;
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2020-04-14 14:57:19 +00:00
|
|
|
for (column = 1, tmp = line; (tmp = strchr(tmp, '\t')); column++) {
|
2019-10-23 13:33:01 +00:00
|
|
|
*tmp++ = '\0';
|
2020-04-14 14:57:19 +00:00
|
|
|
if (*tmp && column == 1)
|
|
|
|
name = tmp;
|
|
|
|
else if (*tmp && column == mod_column)
|
|
|
|
mod = tmp;
|
2019-10-23 13:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:57:19 +00:00
|
|
|
if (!name || !mod)
|
|
|
|
continue;
|
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
symname = strdup(name);
|
|
|
|
if (!symname)
|
|
|
|
perror("strdup");
|
|
|
|
|
2019-04-16 01:21:12 +00:00
|
|
|
objname = make_modname(mod);
|
2017-01-23 20:43:12 +00:00
|
|
|
|
|
|
|
table->exp_syms[i].name = symname;
|
|
|
|
table->exp_syms[i].objname = objname;
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 20:43:12 +00:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:03:14 +00:00
|
|
|
struct lookup_table *lookup_open(char *symtab_path, char *objname,
|
2021-08-02 09:32:23 +00:00
|
|
|
char *symvers_path, struct kpatch_elf *kelf)
|
2017-01-23 20:43:12 +00:00
|
|
|
{
|
|
|
|
struct lookup_table *table;
|
|
|
|
|
|
|
|
table = malloc(sizeof(*table));
|
|
|
|
if (!table)
|
|
|
|
ERROR("malloc table");
|
|
|
|
memset(table, 0, sizeof(*table));
|
|
|
|
|
2019-11-01 20:03:14 +00:00
|
|
|
table->objname = objname;
|
2018-05-11 09:51:00 +00:00
|
|
|
symtab_read(table, symtab_path);
|
2017-01-23 20:43:12 +00:00
|
|
|
symvers_read(table, symvers_path);
|
2021-08-02 09:32:23 +00:00
|
|
|
|
|
|
|
find_local_syms_multiple(table, kelf);
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2014-05-14 20:43:58 +00:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lookup_close(struct lookup_table *table)
|
|
|
|
{
|
2019-04-16 01:21:12 +00:00
|
|
|
int i;
|
|
|
|
struct object_symbol *obj_sym;
|
|
|
|
struct export_symbol *exp_sym;
|
|
|
|
|
|
|
|
for_each_obj_symbol(i, obj_sym, table)
|
|
|
|
free(obj_sym->name);
|
2017-01-23 20:43:12 +00:00
|
|
|
free(table->obj_syms);
|
2019-04-16 01:21:12 +00:00
|
|
|
|
|
|
|
for_each_exp_symbol(i, exp_sym, table) {
|
|
|
|
free(exp_sym->name);
|
|
|
|
free(exp_sym->objname);
|
|
|
|
}
|
2017-01-23 20:43:12 +00:00
|
|
|
free(table->exp_syms);
|
2014-05-14 20:43:58 +00:00
|
|
|
free(table);
|
|
|
|
}
|
|
|
|
|
2021-08-02 09:22:32 +00:00
|
|
|
static bool lookup_local_symbol(struct lookup_table *table,
|
|
|
|
struct symbol *lookup_sym,
|
2018-03-22 20:22:10 +00:00
|
|
|
struct lookup_result *result)
|
2014-05-14 20:43:58 +00:00
|
|
|
{
|
2017-02-04 01:31:25 +00:00
|
|
|
struct object_symbol *sym;
|
2018-04-18 18:41:24 +00:00
|
|
|
unsigned long sympos = 0;
|
2019-11-01 20:19:55 +00:00
|
|
|
int i, in_file = 0;
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2014-05-14 20:43:58 +00:00
|
|
|
memset(result, 0, sizeof(*result));
|
2017-01-23 20:43:12 +00:00
|
|
|
for_each_obj_symbol(i, sym, table) {
|
2021-08-02 09:22:32 +00:00
|
|
|
if (sym->bind == STB_LOCAL && !strcmp(sym->name,
|
|
|
|
lookup_sym->name))
|
2018-04-18 18:41:24 +00:00
|
|
|
sympos++;
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2021-08-02 09:32:23 +00:00
|
|
|
if (lookup_sym->lookup_table_file_sym == sym) {
|
2017-02-04 01:31:25 +00:00
|
|
|
in_file = 1;
|
|
|
|
continue;
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
2017-02-04 01:31:25 +00:00
|
|
|
|
|
|
|
if (!in_file)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sym->type == STT_FILE)
|
|
|
|
break;
|
|
|
|
|
2021-08-02 09:22:32 +00:00
|
|
|
if (sym->bind == STB_LOCAL && !strcmp(sym->name,
|
|
|
|
lookup_sym->name)) {
|
2019-11-01 20:19:55 +00:00
|
|
|
if (result->objname)
|
2021-08-02 09:22:32 +00:00
|
|
|
ERROR("duplicate local symbol found for %s",
|
|
|
|
lookup_sym->name);
|
2019-11-01 20:19:55 +00:00
|
|
|
|
|
|
|
result->objname = table->objname;
|
|
|
|
result->addr = sym->addr;
|
|
|
|
result->size = sym->size;
|
|
|
|
result->sympos = sympos;
|
2018-03-22 20:22:10 +00:00
|
|
|
result->global = false;
|
|
|
|
result->exported = false;
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:19:55 +00:00
|
|
|
return !!result->objname;
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 20:22:10 +00:00
|
|
|
static bool lookup_exported_symbol(struct lookup_table *table, char *name,
|
|
|
|
struct lookup_result *result)
|
|
|
|
{
|
|
|
|
struct export_symbol *sym;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
memset(result, 0, sizeof(*result));
|
|
|
|
|
|
|
|
for_each_exp_symbol(i, sym, table) {
|
|
|
|
if (!strcmp(sym->name, name)) {
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (result->objname)
|
|
|
|
ERROR("duplicate exported symbol found for %s", name);
|
|
|
|
|
|
|
|
result->objname = sym->objname;
|
|
|
|
result->addr = 0; /* determined at runtime */
|
|
|
|
result->size = 0; /* not used for exported symbols */
|
|
|
|
result->sympos = 0; /* always 0 for exported symbols */
|
|
|
|
result->global = true;
|
|
|
|
result->exported = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result && result->objname;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_exported(struct lookup_table *table, char *name)
|
|
|
|
{
|
|
|
|
return lookup_exported_symbol(table, name, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lookup_global_symbol(struct lookup_table *table, char *name,
|
|
|
|
struct lookup_result *result)
|
2014-05-14 20:43:58 +00:00
|
|
|
{
|
2017-01-23 20:43:12 +00:00
|
|
|
struct object_symbol *sym;
|
2014-05-14 20:43:58 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(result, 0, sizeof(*result));
|
2017-01-23 20:43:12 +00:00
|
|
|
for_each_obj_symbol(i, sym, table) {
|
2018-05-11 09:51:00 +00:00
|
|
|
if ((sym->bind == STB_GLOBAL || sym->bind == STB_WEAK) &&
|
2014-05-14 20:43:58 +00:00
|
|
|
!strcmp(sym->name, name)) {
|
2019-11-01 20:19:55 +00:00
|
|
|
|
|
|
|
if (result->objname)
|
|
|
|
ERROR("duplicate global symbol found for %s", name);
|
|
|
|
|
|
|
|
result->objname = table->objname;
|
|
|
|
result->addr = sym->addr;
|
|
|
|
result->size = sym->size;
|
|
|
|
result->sympos = 0; /* always 0 for global symbols */
|
2018-03-22 20:22:10 +00:00
|
|
|
result->global = true;
|
|
|
|
result->exported = is_exported(table, name);
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
2017-01-23 20:43:12 +00:00
|
|
|
}
|
2014-05-14 20:43:58 +00:00
|
|
|
|
2019-11-01 20:19:55 +00:00
|
|
|
return !!result->objname;
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 09:22:32 +00:00
|
|
|
bool lookup_symbol(struct lookup_table *table, struct symbol *sym,
|
2018-03-22 20:22:10 +00:00
|
|
|
struct lookup_result *result)
|
2014-05-14 20:43:58 +00:00
|
|
|
{
|
2021-08-02 09:22:32 +00:00
|
|
|
if (lookup_local_symbol(table, sym, result))
|
2018-03-22 20:22:10 +00:00
|
|
|
return true;
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2021-08-02 09:22:32 +00:00
|
|
|
if (lookup_global_symbol(table, sym->name, result))
|
2018-03-22 20:22:10 +00:00
|
|
|
return true;
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2021-08-02 09:22:32 +00:00
|
|
|
return lookup_exported_symbol(table, sym->name, result);
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|