mirror of
https://github.com/openharmony/third_party_elfutils.git
synced 2026-07-01 06:41:51 -04:00
libdwelf: New DWARF ELF Low-level Functions. Add dwelf_elf_gnu_debuglink.
New public header elfutils/libdwelf.h for low-level DWARF/ELF helper functions. The new function dwelf_elf_gnu_debuglink returns the name and crc as found in the .gnu_debuglink section of an ELF file. Signed-off-by: Mark Wielaard <mjw@redhat.com>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
2014-04-11 Mark Wielaard <mjw@redhat.com>
|
||||
|
||||
* Makefile.am: New file.
|
||||
* libdwelf.h: Likewise.
|
||||
* libdwelfP.h: Likewise.
|
||||
* dwelf_elf_gnu_debuglink.c: Likewise.
|
||||
@@ -0,0 +1,53 @@
|
||||
## Makefile.am for libdwelf library subdirectory in elfutils.
|
||||
##
|
||||
## Process this file with automake to create Makefile.in
|
||||
##
|
||||
## Copyright (C) 2014 Red Hat, Inc.
|
||||
## This file is part of elfutils.
|
||||
##
|
||||
## This file is free software; you can redistribute it and/or modify
|
||||
## it under the terms of either
|
||||
##
|
||||
## * the GNU Lesser General Public License as published by the Free
|
||||
## Software Foundation; either version 3 of the License, or (at
|
||||
## your option) any later version
|
||||
##
|
||||
## or
|
||||
##
|
||||
## * 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
|
||||
##
|
||||
## or both in parallel, as here.
|
||||
##
|
||||
## elfutils 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 copies of the GNU General Public License and
|
||||
## the GNU Lesser General Public License along with this program. If
|
||||
## not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
include $(top_srcdir)/config/eu.am
|
||||
AM_CPPFLAGS += -I$(srcdir)/../libelf -I$(srcdir)/../libdw
|
||||
VERSION = 1
|
||||
|
||||
noinst_LIBRARIES = libdwelf.a libdwelf_pic.a
|
||||
|
||||
pkginclude_HEADERS = libdwelf.h
|
||||
noinst_HEADERS = libdwelfP.h
|
||||
|
||||
libdwelf_a_SOURCES = dwelf_elf_gnu_debuglink.c
|
||||
|
||||
libdwelf = $(libdw)
|
||||
|
||||
libdw = ../libdw/libdw.so
|
||||
libelf = ../libelf/libelf.so
|
||||
libebl = ../libebl/libebl.a
|
||||
libeu = ../lib/libeu.a
|
||||
|
||||
libdwelf_pic_a_SOURCES =
|
||||
am_libdwelf_pic_a_OBJECTS = $(libdwelf_a_SOURCES:.c=.os)
|
||||
|
||||
CLEANFILES += $(am_libdwelf_pic_a_OBJECTS)
|
||||
@@ -0,0 +1,99 @@
|
||||
/* Returns the file name and crc stored in the .gnu_debuglink if found.
|
||||
Copyright (C) 2014 Red Hat, Inc.
|
||||
This file is part of elfutils.
|
||||
|
||||
This file is free software; you can redistribute it and/or modify
|
||||
it under the terms of either
|
||||
|
||||
* the GNU Lesser General Public License as published by the Free
|
||||
Software Foundation; either version 3 of the License, or (at
|
||||
your option) any later version
|
||||
|
||||
or
|
||||
|
||||
* 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
|
||||
|
||||
or both in parallel, as here.
|
||||
|
||||
elfutils 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 copies of the GNU General Public License and
|
||||
the GNU Lesser General Public License along with this program. If
|
||||
not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "libdwelfP.h"
|
||||
|
||||
const char *
|
||||
dwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc)
|
||||
{
|
||||
size_t shstrndx;
|
||||
if (elf_getshdrstrndx (elf, &shstrndx) < 0)
|
||||
return NULL;
|
||||
|
||||
Elf_Scn *scn = NULL;
|
||||
while ((scn = elf_nextscn (elf, scn)) != NULL)
|
||||
{
|
||||
GElf_Shdr shdr_mem;
|
||||
GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
|
||||
if (shdr == NULL)
|
||||
return NULL;
|
||||
|
||||
const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
|
||||
if (!strcmp (name, ".gnu_debuglink"))
|
||||
break;
|
||||
}
|
||||
|
||||
if (scn == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Found the .gnu_debuglink section. Extract its contents. */
|
||||
Elf_Data *rawdata = elf_rawdata (scn, NULL);
|
||||
if (rawdata == NULL)
|
||||
return NULL;
|
||||
|
||||
/* The CRC comes after the zero-terminated file name,
|
||||
(aligned up to 4 bytes) at the end of the section data. */
|
||||
if (rawdata->d_size <= sizeof *crc
|
||||
|| memchr (rawdata->d_buf, '\0', rawdata->d_size - sizeof *crc) == NULL)
|
||||
return NULL;
|
||||
|
||||
Elf_Data crcdata =
|
||||
{
|
||||
.d_type = ELF_T_WORD,
|
||||
.d_buf = crc,
|
||||
.d_size = sizeof *crc,
|
||||
.d_version = EV_CURRENT,
|
||||
};
|
||||
Elf_Data conv =
|
||||
{
|
||||
.d_type = ELF_T_WORD,
|
||||
.d_buf = rawdata->d_buf + rawdata->d_size - sizeof *crc,
|
||||
.d_size = sizeof *crc,
|
||||
.d_version = EV_CURRENT,
|
||||
};
|
||||
|
||||
GElf_Ehdr ehdr_mem;
|
||||
GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
|
||||
if (ehdr == NULL)
|
||||
return NULL;
|
||||
|
||||
Elf_Data *d = gelf_xlatetom (elf, &crcdata, &conv, ehdr->e_ident[EI_DATA]);
|
||||
if (d == NULL)
|
||||
return NULL;
|
||||
assert (d == &crcdata);
|
||||
|
||||
return rawdata->d_buf;
|
||||
}
|
||||
INTDEF(dwelf_elf_gnu_debuglink)
|
||||
@@ -0,0 +1,54 @@
|
||||
/* Interfaces for libdwelf. DWARF ELF Low-level Functions.
|
||||
Copyright (C) 2014 Red Hat, Inc.
|
||||
This file is part of elfutils.
|
||||
|
||||
This file is free software; you can redistribute it and/or modify
|
||||
it under the terms of either
|
||||
|
||||
* the GNU Lesser General Public License as published by the Free
|
||||
Software Foundation; either version 3 of the License, or (at
|
||||
your option) any later version
|
||||
|
||||
or
|
||||
|
||||
* 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
|
||||
|
||||
or both in parallel, as here.
|
||||
|
||||
elfutils 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 copies of the GNU General Public License and
|
||||
the GNU Lesser General Public License along with this program. If
|
||||
not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef _LIBDWELF_H
|
||||
#define _LIBDWELF_H 1
|
||||
|
||||
#include "libdw.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* DWARF ELF Low-level Functions (dwelf).
|
||||
Functions starting with dwelf_elf will take a (libelf) Elf object as
|
||||
first argument and might set elf_errno on error. Functions starting
|
||||
with dwelf_dwarf will take a (libdw) Dwarf object as first argument
|
||||
and might set dwarf_errno on error. */
|
||||
|
||||
/* Returns the name and the CRC32 of the separate debug file from the
|
||||
.gnu_debuglink section if found in the ELF. Return NULL if the ELF
|
||||
file didn't have a .gnu_debuglink section, had malformed data in the
|
||||
section or some other error occured. */
|
||||
extern const char *dwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* libdwelf.h */
|
||||
@@ -0,0 +1,40 @@
|
||||
/* Internal definitions for libdwelf. DWARF ELF Low-level Functions.
|
||||
Copyright (C) 2014 Red Hat, Inc.
|
||||
This file is part of elfutils.
|
||||
|
||||
This file is free software; you can redistribute it and/or modify
|
||||
it under the terms of either
|
||||
|
||||
* the GNU Lesser General Public License as published by the Free
|
||||
Software Foundation; either version 3 of the License, or (at
|
||||
your option) any later version
|
||||
|
||||
or
|
||||
|
||||
* 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
|
||||
|
||||
or both in parallel, as here.
|
||||
|
||||
elfutils 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 copies of the GNU General Public License and
|
||||
the GNU Lesser General Public License along with this program. If
|
||||
not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef _LIBDWELFP_H
|
||||
#define _LIBDWELFP_H 1
|
||||
|
||||
#include <libdwelf.h>
|
||||
#include "../libdw/libdwP.h" /* We need its INTDECLs. */
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Avoid PLT entries. */
|
||||
INTDECL (dwelf_elf_gnu_debuglink)
|
||||
|
||||
#endif /* libdwelfP.h */
|
||||
Reference in New Issue
Block a user