2009-03-24 20:58:44 +01:00
|
|
|
/*
|
2019-01-15 18:28:24 +01:00
|
|
|
SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
2009-03-24 20:58:44 +01:00
|
|
|
Copyright (C) 2009 Red Hat Inc.
|
|
|
|
Copyright (C) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "dutil.h"
|
|
|
|
#include "elf_symtab.h"
|
|
|
|
|
|
|
|
#define HASHSYMS__BITS 8
|
|
|
|
#define HASHSYMS__SIZE (1UL << HASHSYMS__BITS)
|
|
|
|
|
2009-03-25 22:02:48 +01:00
|
|
|
struct elf_symtab *elf_symtab__new(const char *name, Elf *elf, GElf_Ehdr *ehdr)
|
2009-03-24 20:58:44 +01:00
|
|
|
{
|
2009-03-25 22:02:48 +01:00
|
|
|
if (name == NULL)
|
|
|
|
name = ".symtab";
|
|
|
|
|
2009-03-24 20:58:44 +01:00
|
|
|
GElf_Shdr shdr;
|
2009-04-03 16:53:05 +02:00
|
|
|
Elf_Scn *sec = elf_section_by_name(elf, ehdr, &shdr, name, NULL);
|
2009-03-24 20:58:44 +01:00
|
|
|
|
|
|
|
if (sec == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (gelf_getshdr(sec, &shdr) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
struct elf_symtab *symtab = malloc(sizeof(*symtab));
|
|
|
|
if (symtab == NULL)
|
2009-03-24 20:58:44 +01:00
|
|
|
return NULL;
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
symtab->name = strdup(name);
|
|
|
|
if (symtab->name == NULL)
|
2009-03-25 22:02:48 +01:00
|
|
|
goto out_delete;
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
symtab->syms = elf_getdata(sec, NULL);
|
|
|
|
if (symtab->syms == NULL)
|
2009-03-25 22:02:48 +01:00
|
|
|
goto out_free_name;
|
2009-03-24 20:58:44 +01:00
|
|
|
|
|
|
|
sec = elf_getscn(elf, shdr.sh_link);
|
|
|
|
if (sec == NULL)
|
2009-03-25 22:02:48 +01:00
|
|
|
goto out_free_name;
|
2009-03-24 20:58:44 +01:00
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
symtab->symstrs = elf_getdata(sec, NULL);
|
|
|
|
if (symtab->symstrs == NULL)
|
2009-03-25 22:02:48 +01:00
|
|
|
goto out_free_name;
|
2009-03-24 20:58:44 +01:00
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
symtab->nr_syms = shdr.sh_size / shdr.sh_entsize;
|
2009-03-24 20:58:44 +01:00
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
return symtab;
|
2009-03-25 22:02:48 +01:00
|
|
|
out_free_name:
|
2012-08-17 23:47:15 +02:00
|
|
|
free(symtab->name);
|
2009-03-24 20:58:44 +01:00
|
|
|
out_delete:
|
2012-08-17 23:47:15 +02:00
|
|
|
free(symtab);
|
2009-03-24 20:58:44 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-17 23:47:15 +02:00
|
|
|
void elf_symtab__delete(struct elf_symtab *symtab)
|
2009-03-24 20:58:44 +01:00
|
|
|
{
|
2012-08-17 23:47:15 +02:00
|
|
|
if (symtab == NULL)
|
2009-04-03 14:41:09 +02:00
|
|
|
return;
|
2012-08-17 23:47:15 +02:00
|
|
|
free(symtab->name);
|
|
|
|
free(symtab);
|
2009-03-24 20:58:44 +01:00
|
|
|
}
|