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>
|
|
|
|
#include <error.h>
|
|
|
|
#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;
|
2017-02-04 01:31:25 +00:00
|
|
|
struct object_symbol *local_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
|
|
|
|
2018-03-21 17:47:58 +00:00
|
|
|
static int maybe_discarded_sym(const char *name)
|
2017-03-03 14:27:28 +00:00
|
|
|
{
|
2018-03-21 17:47:58 +00:00
|
|
|
if (!name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) ||
|
lookup: Add __UNIQUE_ID_ to maybe_discarded_sym list
Linux kernel tristate config options allows selected feature, either to
be built-in to the kernel or to be built as a kernel module. When built
as a kernel module, it's expected that the module, will be built with
module information such as author, license, description and others.
For each of the modinfo, a corresponding __UNIQUE_ID_ symbol is
generated. Their lookup succeeds in the case of module but fails when
selected to built-in to the kernel, the reason being that the kernel
discards these __UNIQUE_ID_ symbols during linking. Add __UNIQUE_ID_
symbols to maybe_discarded_sym list, to avoid failure in case of
table->object is vmlinux.
i.e.:
# cat .config|grep IOSCHED_BFQ (can be compiled as module too)
CONFIG_IOSCHED_BFQ=y
# readelf -sW ./block/bfq-iosched.o|grep UNIQUE
219: 0000000000000000 54 OBJECT LOCAL DEFAULT 267 __UNIQUE_ID_description223
220: 0000000000000036 16 OBJECT LOCAL DEFAULT 267 __UNIQUE_ID_license222
221: 0000000000000046 19 OBJECT LOCAL DEFAULT 267 __UNIQUE_ID_file221
222: 0000000000000059 25 OBJECT LOCAL DEFAULT 267 __UNIQUE_ID_author220
223: 0000000000000072 22 OBJECT LOCAL DEFAULT 267 __UNIQUE_ID_alias219
the line below before the kpatch error, is a debug printf to find the failing lookup symbol:
Failed lookup for __UNIQUE_ID_description223
/root/kpatch/kpatch-build/create-diff-object: ERROR: bfq-iosched.o: find_local_syms: 180: couldn't find matching bfq-iosched.c local symbols in ./vmlinux symbol table
with the patch, it successfully builds with both y/m config options:
...
bfq-iosched.o: changed function: bfq_idle_slice_timer
Patched objects: vmlinux
Building patch module:
livepatch-0001-block-bfq-fix-use-after-free-in-b.ko
SUCCESS
Signed-off-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com>
2020-06-17 14:21:56 +00:00
|
|
|
!strncmp(name, "__addressable_", 14) ||
|
|
|
|
!strncmp(name, "__UNIQUE_ID_", 12))
|
2017-03-03 14:27:28 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
static int locals_match(struct lookup_table *table, int idx,
|
|
|
|
struct sym_compare_type *child_locals)
|
2017-02-04 01:31:25 +00:00
|
|
|
{
|
2017-07-06 17:44:50 +00:00
|
|
|
struct sym_compare_type *child;
|
|
|
|
struct object_symbol *sym;
|
|
|
|
int i, found;
|
|
|
|
|
|
|
|
i = idx + 1;
|
|
|
|
for_each_obj_symbol_continue(i, sym, table) {
|
|
|
|
if (sym->type == STT_FILE)
|
|
|
|
break;
|
|
|
|
if (sym->bind != STB_LOCAL)
|
|
|
|
continue;
|
|
|
|
if (sym->type != STT_FUNC && sym->type != STT_OBJECT)
|
|
|
|
continue;
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
found = 0;
|
|
|
|
for (child = child_locals; child->name; child++) {
|
|
|
|
if (child->type == sym->type &&
|
|
|
|
!strcmp(child->name, sym->name)) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
if (!found)
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-04 01:31:25 +00:00
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
for (child = child_locals; child->name; child++) {
|
2017-03-03 14:27:28 +00:00
|
|
|
/*
|
|
|
|
* Symbols which get discarded at link time are missing from
|
|
|
|
* the lookup table, so skip them.
|
|
|
|
*/
|
2018-03-21 17:47:58 +00:00
|
|
|
if (maybe_discarded_sym(child->name))
|
2017-07-06 17:44:50 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
found = 0;
|
|
|
|
i = idx + 1;
|
|
|
|
for_each_obj_symbol_continue(i, sym, table) {
|
|
|
|
if (sym->type == STT_FILE)
|
|
|
|
break;
|
|
|
|
if (sym->bind != STB_LOCAL)
|
|
|
|
continue;
|
|
|
|
if (sym->type != STT_FUNC && sym->type != STT_OBJECT)
|
|
|
|
continue;
|
2018-03-21 17:47:58 +00:00
|
|
|
if (maybe_discarded_sym(sym->name))
|
|
|
|
continue;
|
2017-07-06 17:44:50 +00:00
|
|
|
|
|
|
|
if (!strcmp(child->name, sym->name)) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return 0;
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 17:44:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_local_syms(struct lookup_table *table, char *hint,
|
|
|
|
struct sym_compare_type *child_locals)
|
|
|
|
{
|
|
|
|
struct object_symbol *sym;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!child_locals)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for_each_obj_symbol(i, sym, table) {
|
|
|
|
if (sym->type != STT_FILE)
|
|
|
|
continue;
|
|
|
|
if (strcmp(hint, sym->name))
|
|
|
|
continue;
|
|
|
|
if (!locals_match(table, i, child_locals))
|
|
|
|
continue;
|
2017-02-04 01:31:25 +00:00
|
|
|
if (table->local_syms)
|
2019-11-01 20:03:14 +00:00
|
|
|
ERROR("found duplicate matches for %s local symbols in %s symbol table",
|
|
|
|
hint, table->objname);
|
2017-07-06 17:44:50 +00:00
|
|
|
|
|
|
|
table->local_syms = sym;
|
2017-02-04 01:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!table->local_syms)
|
2019-11-01 20:03:14 +00:00
|
|
|
ERROR("couldn't find matching %s local symbols in %s symbol table",
|
|
|
|
hint, table->objname);
|
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.
|
|
|
|
*/
|
|
|
|
if (strstr(line, ".dynsym")) {
|
|
|
|
skip = true;
|
|
|
|
continue;
|
|
|
|
} else if (strstr(line, ".symtab")) {
|
|
|
|
skip = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
char *symvers_path, char *hint,
|
|
|
|
struct sym_compare_type *locals)
|
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);
|
2017-03-03 23:06:16 +00:00
|
|
|
find_local_syms(table, hint, locals);
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:22:10 +00:00
|
|
|
static bool lookup_local_symbol(struct lookup_table *table, char *name,
|
|
|
|
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
|
|
|
|
|
|
|
if (!table->local_syms)
|
2019-11-01 19:59:05 +00:00
|
|
|
return false;
|
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) {
|
2017-02-04 01:31:25 +00:00
|
|
|
if (sym->bind == STB_LOCAL && !strcmp(sym->name, name))
|
2018-04-18 18:41:24 +00:00
|
|
|
sympos++;
|
2017-02-04 01:31:25 +00:00
|
|
|
|
|
|
|
if (table->local_syms == sym) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (sym->bind == STB_LOCAL && !strcmp(sym->name, name)) {
|
2019-11-01 20:19:55 +00:00
|
|
|
|
|
|
|
if (result->objname)
|
|
|
|
ERROR("duplicate local symbol found for %s", name);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-22 20:22:10 +00:00
|
|
|
bool lookup_symbol(struct lookup_table *table, char *name,
|
|
|
|
struct lookup_result *result)
|
2014-05-14 20:43:58 +00:00
|
|
|
{
|
2018-03-22 20:22:10 +00:00
|
|
|
if (lookup_local_symbol(table, name, result))
|
|
|
|
return true;
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2018-03-22 20:22:10 +00:00
|
|
|
if (lookup_global_symbol(table, name, result))
|
|
|
|
return true;
|
2017-01-23 20:43:12 +00:00
|
|
|
|
2018-03-22 20:22:10 +00:00
|
|
|
return lookup_exported_symbol(table, name, result);
|
2014-05-14 20:43:58 +00:00
|
|
|
}
|