ext: Update libelf from elftoolchain 0.7.1
Change-Id: I8f86cd918ad01897c42aa479f9c64520def36830 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/50927 Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Reviewed-by: Austin Harris <mail@austin-harris.com> Reviewed-by: Hoa Nguyen <hoanguyen@ucdavis.edu> Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu> Maintainer: Jason Lowe-Power <power.jg@gmail.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -36,6 +36,7 @@ elf_files = []
|
||||
def ElfFile(filename):
|
||||
elf_files.append(File(filename))
|
||||
|
||||
ElfFile('elf.c')
|
||||
ElfFile('elf_begin.c')
|
||||
ElfFile('elf_cntl.c')
|
||||
ElfFile('elf_data.c')
|
||||
@@ -52,6 +53,7 @@ ElfFile('elf_hash.c')
|
||||
ElfFile('elf_kind.c')
|
||||
ElfFile('elf_memory.c')
|
||||
ElfFile('elf_next.c')
|
||||
ElfFile('elf_open.c')
|
||||
ElfFile('elf_phnum.c')
|
||||
ElfFile('elf_rand.c')
|
||||
ElfFile('elf_rawfile.c')
|
||||
@@ -61,26 +63,32 @@ ElfFile('elf_shstrndx.c')
|
||||
ElfFile('elf_strptr.c')
|
||||
ElfFile('elf_update.c')
|
||||
ElfFile('elf_version.c')
|
||||
ElfFile('gelf_cap.c')
|
||||
ElfFile('gelf_checksum.c')
|
||||
ElfFile('gelf_dyn.c')
|
||||
ElfFile('gelf_ehdr.c')
|
||||
ElfFile('gelf_fsize.c')
|
||||
ElfFile('gelf_getclass.c')
|
||||
ElfFile('gelf_move.c')
|
||||
ElfFile('gelf_phdr.c')
|
||||
ElfFile('gelf_rel.c')
|
||||
ElfFile('gelf_rela.c')
|
||||
ElfFile('gelf_shdr.c')
|
||||
ElfFile('gelf_sym.c')
|
||||
ElfFile('gelf_syminfo.c')
|
||||
ElfFile('gelf_symshndx.c')
|
||||
ElfFile('gelf_xlate.c')
|
||||
ElfFile('libelf.c')
|
||||
ElfFile('libelf_align.c')
|
||||
ElfFile('libelf_allocate.c')
|
||||
ElfFile('libelf_ar.c')
|
||||
ElfFile('libelf_ar_util.c')
|
||||
ElfFile('libelf_checksum.c')
|
||||
ElfFile('libelf_data.c')
|
||||
ElfFile('libelf_ehdr.c')
|
||||
ElfFile('libelf_extended.c')
|
||||
ElfFile('libelf_memory.c')
|
||||
ElfFile('libelf_open.c')
|
||||
ElfFile('libelf_phdr.c')
|
||||
ElfFile('libelf_shdr.c')
|
||||
ElfFile('libelf_xlate.c')
|
||||
@@ -102,7 +110,7 @@ if m4env['CLANG']:
|
||||
# linking problems with multiple definitions of the symbols in
|
||||
# sysmacros.h for older versions of glibc
|
||||
m4env.Append(CCFLAGS=['-std=gnu89'])
|
||||
m4env.Append(CCFLAGS=['-Wno-implicit'])
|
||||
m4env.Append(CCFLAGS=['-Wno-implicit', '-Wno-undef'])
|
||||
del m4env['CPPPATH']
|
||||
|
||||
# If we have gm4 use it
|
||||
@@ -128,11 +136,16 @@ m4env.M4(target=File('libelf_fsize.c'),
|
||||
m4env.M4(target=File('libelf_msize.c'),
|
||||
source=[File('elf_types.m4'), File('libelf_msize.m4')])
|
||||
|
||||
m4env.Append(CPPPATH=Dir('.'))
|
||||
|
||||
# Build libelf as a static library with PIC code so it can be linked
|
||||
# into either m5 or the library
|
||||
m4env.Library('elf', [m4env.SharedObject(f) for f in elf_files])
|
||||
|
||||
# Generate the native-elf-format header file based on the build system
|
||||
m4env.Command(File('native-elf-format.h'), File('native-elf-format'),
|
||||
'${SOURCE} > ${TARGET}')
|
||||
|
||||
main.Prepend(CPPPATH=Dir('.'))
|
||||
main.Append(LIBS=[File('libelf.a')])
|
||||
main.Prepend(LIBPATH=[Dir('.')])
|
||||
|
||||
|
||||
484
ext/libelf/_elftc.h
Normal file
484
ext/libelf/_elftc.h
Normal file
@@ -0,0 +1,484 @@
|
||||
/*-
|
||||
* Copyright (c) 2009 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: _elftc.h 3244 2015-08-31 19:53:08Z emaste $
|
||||
*/
|
||||
|
||||
/**
|
||||
** Miscellanous definitions needed by multiple components.
|
||||
**/
|
||||
|
||||
#ifndef _ELFTC_H
|
||||
#define _ELFTC_H
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void *) 0)
|
||||
#endif
|
||||
|
||||
#ifndef offsetof
|
||||
#define offsetof(T, M) ((int) &((T*) 0) -> M)
|
||||
#endif
|
||||
|
||||
/* --QUEUE-MACROS-- [[ */
|
||||
|
||||
/*
|
||||
* Supply macros missing from <sys/queue.h>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1991, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef LIST_FOREACH_SAFE
|
||||
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = LIST_FIRST((head)); \
|
||||
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
#endif
|
||||
|
||||
#ifndef SLIST_FOREACH_SAFE
|
||||
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = SLIST_FIRST((head)); \
|
||||
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_CONCAT
|
||||
#define STAILQ_CONCAT(head1, head2) do { \
|
||||
if (!STAILQ_EMPTY((head2))) { \
|
||||
*(head1)->stqh_last = (head2)->stqh_first; \
|
||||
(head1)->stqh_last = (head2)->stqh_last; \
|
||||
STAILQ_INIT((head2)); \
|
||||
} \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_EMPTY
|
||||
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_ENTRY
|
||||
#define STAILQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *stqe_next; /* next element */ \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_FIRST
|
||||
#define STAILQ_FIRST(head) ((head)->stqh_first)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_HEAD
|
||||
#define STAILQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *stqh_first; /* first element */ \
|
||||
struct type **stqh_last; /* addr of last next element */ \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_HEAD_INITIALIZER
|
||||
#define STAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).stqh_first }
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_FOREACH
|
||||
#define STAILQ_FOREACH(var, head, field) \
|
||||
for ((var) = ((head)->stqh_first); \
|
||||
(var); \
|
||||
(var) = ((var)->field.stqe_next))
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_FOREACH_SAFE
|
||||
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = STAILQ_FIRST((head)); \
|
||||
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_INIT
|
||||
#define STAILQ_INIT(head) do { \
|
||||
(head)->stqh_first = NULL; \
|
||||
(head)->stqh_last = &(head)->stqh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_INSERT_HEAD
|
||||
#define STAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \
|
||||
(head)->stqh_last = &(elm)->field.stqe_next; \
|
||||
(head)->stqh_first = (elm); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_INSERT_TAIL
|
||||
#define STAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||
(elm)->field.stqe_next = NULL; \
|
||||
*(head)->stqh_last = (elm); \
|
||||
(head)->stqh_last = &(elm)->field.stqe_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_INSERT_AFTER
|
||||
#define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
|
||||
(head)->stqh_last = &(elm)->field.stqe_next; \
|
||||
(listelm)->field.stqe_next = (elm); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_LAST
|
||||
#define STAILQ_LAST(head, type, field) \
|
||||
(STAILQ_EMPTY((head)) ? \
|
||||
NULL : ((struct type *)(void *) \
|
||||
((char *)((head)->stqh_last) - offsetof(struct type, field))))
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_NEXT
|
||||
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_REMOVE
|
||||
#define STAILQ_REMOVE(head, elm, type, field) do { \
|
||||
if ((head)->stqh_first == (elm)) { \
|
||||
STAILQ_REMOVE_HEAD((head), field); \
|
||||
} else { \
|
||||
struct type *curelm = (head)->stqh_first; \
|
||||
while (curelm->field.stqe_next != (elm)) \
|
||||
curelm = curelm->field.stqe_next; \
|
||||
if ((curelm->field.stqe_next = \
|
||||
curelm->field.stqe_next->field.stqe_next) == NULL) \
|
||||
(head)->stqh_last = &(curelm)->field.stqe_next; \
|
||||
} \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef STAILQ_REMOVE_HEAD
|
||||
#define STAILQ_REMOVE_HEAD(head, field) do { \
|
||||
if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \
|
||||
NULL) \
|
||||
(head)->stqh_last = &(head)->stqh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* The STAILQ_SORT macro is adapted from Simon Tatham's O(n*log(n))
|
||||
* mergesort algorithm.
|
||||
*/
|
||||
#ifndef STAILQ_SORT
|
||||
#define STAILQ_SORT(head, type, field, cmp) do { \
|
||||
STAILQ_HEAD(, type) _la, _lb; \
|
||||
struct type *_p, *_q, *_e; \
|
||||
int _i, _sz, _nmerges, _psz, _qsz; \
|
||||
\
|
||||
_sz = 1; \
|
||||
do { \
|
||||
_nmerges = 0; \
|
||||
STAILQ_INIT(&_lb); \
|
||||
while (!STAILQ_EMPTY((head))) { \
|
||||
_nmerges++; \
|
||||
STAILQ_INIT(&_la); \
|
||||
_psz = 0; \
|
||||
for (_i = 0; _i < _sz && !STAILQ_EMPTY((head)); \
|
||||
_i++) { \
|
||||
_e = STAILQ_FIRST((head)); \
|
||||
if (_e == NULL) \
|
||||
break; \
|
||||
_psz++; \
|
||||
STAILQ_REMOVE_HEAD((head), field); \
|
||||
STAILQ_INSERT_TAIL(&_la, _e, field); \
|
||||
} \
|
||||
_p = STAILQ_FIRST(&_la); \
|
||||
_qsz = _sz; \
|
||||
_q = STAILQ_FIRST((head)); \
|
||||
while (_psz > 0 || (_qsz > 0 && _q != NULL)) { \
|
||||
if (_psz == 0) { \
|
||||
_e = _q; \
|
||||
_q = STAILQ_NEXT(_q, field); \
|
||||
STAILQ_REMOVE_HEAD((head), \
|
||||
field); \
|
||||
_qsz--; \
|
||||
} else if (_qsz == 0 || _q == NULL) { \
|
||||
_e = _p; \
|
||||
_p = STAILQ_NEXT(_p, field); \
|
||||
STAILQ_REMOVE_HEAD(&_la, field);\
|
||||
_psz--; \
|
||||
} else if (cmp(_p, _q) <= 0) { \
|
||||
_e = _p; \
|
||||
_p = STAILQ_NEXT(_p, field); \
|
||||
STAILQ_REMOVE_HEAD(&_la, field);\
|
||||
_psz--; \
|
||||
} else { \
|
||||
_e = _q; \
|
||||
_q = STAILQ_NEXT(_q, field); \
|
||||
STAILQ_REMOVE_HEAD((head), \
|
||||
field); \
|
||||
_qsz--; \
|
||||
} \
|
||||
STAILQ_INSERT_TAIL(&_lb, _e, field); \
|
||||
} \
|
||||
} \
|
||||
(head)->stqh_first = _lb.stqh_first; \
|
||||
(head)->stqh_last = _lb.stqh_last; \
|
||||
_sz *= 2; \
|
||||
} while (_nmerges > 1); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
#endif
|
||||
|
||||
#ifndef TAILQ_FOREACH_SAFE
|
||||
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = TAILQ_FIRST((head)); \
|
||||
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
#endif
|
||||
|
||||
/* ]] --QUEUE-MACROS-- */
|
||||
|
||||
/*
|
||||
* VCS Ids.
|
||||
*/
|
||||
|
||||
#ifndef ELFTC_VCSID
|
||||
|
||||
#if defined(__DragonFly__)
|
||||
#define ELFTC_VCSID(ID) __RCSID(ID)
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
#define ELFTC_VCSID(ID) __FBSDID(ID)
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__GLIBC__) || defined(__GNU__) || \
|
||||
defined(__linux__)
|
||||
#if defined(__GNUC__)
|
||||
#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")
|
||||
#else
|
||||
#define ELFTC_VCSID(ID) /**/
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__minix)
|
||||
#if defined(__GNUC__)
|
||||
#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")
|
||||
#else
|
||||
#define ELFTC_VCSID(ID) /**/
|
||||
#endif /* __GNU__ */
|
||||
#endif
|
||||
|
||||
#if defined(__NetBSD__)
|
||||
#define ELFTC_VCSID(ID) __RCSID(ID)
|
||||
#endif
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
#if defined(__GNUC__)
|
||||
#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")
|
||||
#else
|
||||
#define ELFTC_VCSID(ID) /**/
|
||||
#endif /* __GNUC__ */
|
||||
#endif
|
||||
|
||||
#endif /* ELFTC_VCSID */
|
||||
|
||||
/*
|
||||
* Provide an equivalent for getprogname(3).
|
||||
*/
|
||||
|
||||
#ifndef ELFTC_GETPROGNAME
|
||||
|
||||
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
|
||||
defined(__minix) || defined(__NetBSD__)
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ELFTC_GETPROGNAME() getprogname()
|
||||
|
||||
#endif /* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
|
||||
|
||||
|
||||
#if defined(__GLIBC__) || defined(__linux__)
|
||||
#ifndef _GNU_SOURCE
|
||||
/*
|
||||
* GLIBC based systems have a global 'char *' pointer referencing
|
||||
* the executable's name.
|
||||
*/
|
||||
extern const char *program_invocation_short_name;
|
||||
#endif /* !_GNU_SOURCE */
|
||||
|
||||
#define ELFTC_GETPROGNAME() program_invocation_short_name
|
||||
|
||||
#endif /* __GLIBC__ || __linux__ */
|
||||
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
|
||||
extern const char *__progname;
|
||||
|
||||
#define ELFTC_GETPROGNAME() __progname
|
||||
|
||||
#endif /* __OpenBSD__ */
|
||||
|
||||
#endif /* ELFTC_GETPROGNAME */
|
||||
|
||||
|
||||
/**
|
||||
** Per-OS configuration.
|
||||
**/
|
||||
|
||||
#if defined(__APPLE__)
|
||||
|
||||
#include <libkern/OSByteOrder.h>
|
||||
#define htobe32(x) OSSwapHostToBigInt32(x)
|
||||
#define roundup2 roundup
|
||||
|
||||
#define ELFTC_BYTE_ORDER _BYTE_ORDER
|
||||
#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
|
||||
#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
|
||||
|
||||
#define ELFTC_HAVE_MMAP 1
|
||||
#define ELFTC_HAVE_STRMODE 1
|
||||
|
||||
#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
|
||||
#if defined(__DragonFly__)
|
||||
|
||||
#include <osreldate.h>
|
||||
#include <sys/endian.h>
|
||||
|
||||
#define ELFTC_BYTE_ORDER _BYTE_ORDER
|
||||
#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
|
||||
#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
|
||||
|
||||
#define ELFTC_HAVE_MMAP 1
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__GLIBC__) || defined(__linux__)
|
||||
|
||||
#include <endian.h>
|
||||
|
||||
#define ELFTC_BYTE_ORDER __BYTE_ORDER
|
||||
#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN __LITTLE_ENDIAN
|
||||
#define ELFTC_BYTE_ORDER_BIG_ENDIAN __BIG_ENDIAN
|
||||
|
||||
#define ELFTC_HAVE_MMAP 1
|
||||
|
||||
/*
|
||||
* Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
|
||||
*/
|
||||
#define ELFTC_HAVE_STRMODE 0
|
||||
|
||||
/* Whether we need to supply {be,le}32dec. */
|
||||
#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1
|
||||
|
||||
#define roundup2 roundup
|
||||
|
||||
#endif /* __GLIBC__ || __linux__ */
|
||||
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
|
||||
#include <osreldate.h>
|
||||
#include <sys/endian.h>
|
||||
|
||||
#define ELFTC_BYTE_ORDER _BYTE_ORDER
|
||||
#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
|
||||
#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
|
||||
|
||||
#define ELFTC_HAVE_MMAP 1
|
||||
#define ELFTC_HAVE_STRMODE 1
|
||||
#if __FreeBSD_version <= 900000
|
||||
#define ELFTC_BROKEN_YY_NO_INPUT 1
|
||||
#endif
|
||||
#endif /* __FreeBSD__ */
|
||||
|
||||
|
||||
#if defined(__minix)
|
||||
#define ELFTC_HAVE_MMAP 0
|
||||
#endif /* __minix */
|
||||
|
||||
|
||||
#if defined(__NetBSD__)
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/endian.h>
|
||||
|
||||
#define ELFTC_BYTE_ORDER _BYTE_ORDER
|
||||
#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
|
||||
#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
|
||||
|
||||
#define ELFTC_HAVE_MMAP 1
|
||||
#define ELFTC_HAVE_STRMODE 1
|
||||
#if __NetBSD_Version__ <= 599002100
|
||||
/* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */
|
||||
/* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */
|
||||
# define ELFTC_BROKEN_YY_NO_INPUT 1
|
||||
#endif
|
||||
#endif /* __NetBSD __ */
|
||||
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/endian.h>
|
||||
|
||||
#define ELFTC_BYTE_ORDER _BYTE_ORDER
|
||||
#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
|
||||
#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
|
||||
|
||||
#define ELFTC_HAVE_MMAP 1
|
||||
#define ELFTC_HAVE_STRMODE 1
|
||||
|
||||
#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1
|
||||
#define roundup2 roundup
|
||||
|
||||
#endif /* __OpenBSD__ */
|
||||
|
||||
#endif /* _ELFTC_H */
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -23,18 +23,17 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/_libelf.h,v 1.2 2006/12/25 02:22:22 jkoshy Exp $
|
||||
* $Id: _libelf.h 3174 2015-03-27 17:13:41Z emaste $
|
||||
*/
|
||||
|
||||
#ifndef __LIBELF_H_
|
||||
#define __LIBELF_H_
|
||||
|
||||
#include "elf_queue.h"
|
||||
#include "libelf.h"
|
||||
#include <sys/queue.h>
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void *) 0)
|
||||
#endif
|
||||
#include "_libelf_config.h"
|
||||
|
||||
#include "_elftc.h"
|
||||
|
||||
/*
|
||||
* Library-private data structures.
|
||||
@@ -43,144 +42,187 @@
|
||||
#define LIBELF_MSG_SIZE 256
|
||||
|
||||
struct _libelf_globals {
|
||||
int libelf_arch;
|
||||
unsigned int libelf_byteorder;
|
||||
int libelf_class;
|
||||
int libelf_error;
|
||||
int libelf_fillchar;
|
||||
unsigned int libelf_version;
|
||||
char libelf_msg[LIBELF_MSG_SIZE];
|
||||
int libelf_arch;
|
||||
unsigned int libelf_byteorder;
|
||||
int libelf_class;
|
||||
int libelf_error;
|
||||
int libelf_fillchar;
|
||||
unsigned int libelf_version;
|
||||
unsigned char libelf_msg[LIBELF_MSG_SIZE];
|
||||
};
|
||||
|
||||
struct _libelf_globals *_libelf_private();
|
||||
extern struct _libelf_globals _libelf;
|
||||
|
||||
#define LIBELF_PRIVATE(N) (_libelf_private()->libelf_##N)
|
||||
#define LIBELF_PRIVATE(N) (_libelf.libelf_##N)
|
||||
|
||||
#define LIBELF_ELF_ERROR_MASK 0xFF
|
||||
#define LIBELF_OS_ERROR_SHIFT 8
|
||||
#define LIBELF_ELF_ERROR_MASK 0xFF
|
||||
#define LIBELF_OS_ERROR_SHIFT 8
|
||||
|
||||
#define LIBELF_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) | \
|
||||
((O) << LIBELF_OS_ERROR_SHIFT))
|
||||
|
||||
#define LIBELF_SET_ERROR(E, O) do { \
|
||||
LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \
|
||||
((O) << LIBELF_OS_ERROR_SHIFT)); \
|
||||
} while (0)
|
||||
LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O)); \
|
||||
} while (0)
|
||||
|
||||
#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U)
|
||||
|
||||
/*
|
||||
* Flags for library internal use. These use the upper 16 bits of a
|
||||
* flags field.
|
||||
* Flags for library internal use. These use the upper 16 bits of the
|
||||
* `e_flags' field.
|
||||
*/
|
||||
#define LIBELF_F_MALLOCED 0x010000 /* whether data was malloc'ed */
|
||||
#define LIBELF_F_MMAP 0x020000 /* whether e_rawfile was mmap'ed */
|
||||
#define LIBELF_F_SHDRS_LOADED 0x040000 /* whether all shdrs were read in */
|
||||
#define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */
|
||||
#define LIBELF_F_AR_HEADER 0x010000U /* translated header available */
|
||||
#define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
|
||||
#define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */
|
||||
#define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */
|
||||
#define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */
|
||||
#define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */
|
||||
#define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */
|
||||
|
||||
struct _Elf {
|
||||
int e_activations; /* activation count */
|
||||
Elf_Arhdr *e_arhdr; /* header for archive members */
|
||||
unsigned int e_byteorder; /* ELFDATA* */
|
||||
int e_class; /* ELFCLASS* */
|
||||
Elf_Cmd e_cmd; /* ELF_C_* used at creation time */
|
||||
int e_fd; /* associated file descriptor */
|
||||
unsigned int e_flags; /* ELF_F_*, LIBELF_F_* flags */
|
||||
Elf_Kind e_kind; /* ELF_K_* */
|
||||
Elf *e_parent; /* non-NULL for archive members */
|
||||
char *e_rawfile; /* uninterpreted bytes */
|
||||
size_t e_rawsize; /* size of uninterpreted bytes */
|
||||
unsigned int e_version; /* file version */
|
||||
int e_activations; /* activation count */
|
||||
unsigned int e_byteorder; /* ELFDATA* */
|
||||
int e_class; /* ELFCLASS* */
|
||||
Elf_Cmd e_cmd; /* ELF_C_* used at creation time */
|
||||
int e_fd; /* associated file descriptor */
|
||||
unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */
|
||||
Elf_Kind e_kind; /* ELF_K_* */
|
||||
Elf *e_parent; /* non-NULL for archive members */
|
||||
unsigned char *e_rawfile; /* uninterpreted bytes */
|
||||
size_t e_rawsize; /* size of uninterpreted bytes */
|
||||
unsigned int e_version; /* file version */
|
||||
|
||||
union {
|
||||
struct { /* ar(1) archives */
|
||||
off_t e_next; /* set by elf_rand()/elf_next() */
|
||||
int e_nchildren;
|
||||
char *e_rawstrtab; /* file name strings */
|
||||
size_t e_rawstrtabsz;
|
||||
char *e_rawsymtab; /* symbol table */
|
||||
size_t e_rawsymtabsz;
|
||||
Elf_Arsym *e_symtab;
|
||||
size_t e_symtabsz;
|
||||
} e_ar;
|
||||
struct { /* regular ELF files */
|
||||
union {
|
||||
Elf32_Ehdr *e_ehdr32;
|
||||
Elf64_Ehdr *e_ehdr64;
|
||||
} e_ehdr;
|
||||
union {
|
||||
Elf32_Phdr *e_phdr32;
|
||||
Elf64_Phdr *e_phdr64;
|
||||
} e_phdr;
|
||||
STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */
|
||||
size_t e_nphdr; /* number of Phdr entries */
|
||||
size_t e_nscn; /* number of sections */
|
||||
size_t e_strndx; /* string table section index */
|
||||
} e_elf;
|
||||
} e_u;
|
||||
/*
|
||||
* Header information for archive members. See the
|
||||
* LIBELF_F_AR_HEADER flag.
|
||||
*/
|
||||
union {
|
||||
Elf_Arhdr *e_arhdr; /* translated header */
|
||||
unsigned char *e_rawhdr; /* untranslated header */
|
||||
} e_hdr;
|
||||
|
||||
union {
|
||||
struct { /* ar(1) archives */
|
||||
off_t e_next; /* set by elf_rand()/elf_next() */
|
||||
int e_nchildren;
|
||||
unsigned char *e_rawstrtab; /* file name strings */
|
||||
size_t e_rawstrtabsz;
|
||||
unsigned char *e_rawsymtab; /* symbol table */
|
||||
size_t e_rawsymtabsz;
|
||||
Elf_Arsym *e_symtab;
|
||||
size_t e_symtabsz;
|
||||
} e_ar;
|
||||
struct { /* regular ELF files */
|
||||
union {
|
||||
Elf32_Ehdr *e_ehdr32;
|
||||
Elf64_Ehdr *e_ehdr64;
|
||||
} e_ehdr;
|
||||
union {
|
||||
Elf32_Phdr *e_phdr32;
|
||||
Elf64_Phdr *e_phdr64;
|
||||
} e_phdr;
|
||||
STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */
|
||||
size_t e_nphdr; /* number of Phdr entries */
|
||||
size_t e_nscn; /* number of sections */
|
||||
size_t e_strndx; /* string table section index */
|
||||
} e_elf;
|
||||
} e_u;
|
||||
};
|
||||
|
||||
/*
|
||||
* The internal descriptor wrapping the "Elf_Data" type.
|
||||
*/
|
||||
struct _Libelf_Data {
|
||||
Elf_Data d_data; /* The exported descriptor. */
|
||||
Elf_Scn *d_scn; /* The containing section */
|
||||
unsigned int d_flags;
|
||||
STAILQ_ENTRY(_Libelf_Data) d_next;
|
||||
};
|
||||
|
||||
struct _Elf_Scn {
|
||||
union {
|
||||
Elf32_Shdr s_shdr32;
|
||||
Elf64_Shdr s_shdr64;
|
||||
} s_shdr;
|
||||
STAILQ_HEAD(, _Elf_Data) s_data; /* list of Elf_Data descriptors */
|
||||
STAILQ_HEAD(, _Elf_Data) s_rawdata; /* raw data for this section */
|
||||
STAILQ_ENTRY(_Elf_Scn) s_next;
|
||||
struct _Elf *s_elf; /* parent ELF descriptor */
|
||||
unsigned int s_flags; /* flags for the section as a whole */
|
||||
size_t s_ndx; /* index# for this section */
|
||||
uint64_t s_offset; /* managed by elf_update() */
|
||||
uint64_t s_rawoff; /* original offset in the file */
|
||||
uint64_t s_size; /* managed by elf_update() */
|
||||
union {
|
||||
Elf32_Shdr s_shdr32;
|
||||
Elf64_Shdr s_shdr64;
|
||||
} s_shdr;
|
||||
STAILQ_HEAD(, _Libelf_Data) s_data; /* translated data */
|
||||
STAILQ_HEAD(, _Libelf_Data) s_rawdata; /* raw data */
|
||||
STAILQ_ENTRY(_Elf_Scn) s_next;
|
||||
struct _Elf *s_elf; /* parent ELF descriptor */
|
||||
unsigned int s_flags; /* flags for the section as a whole */
|
||||
size_t s_ndx; /* index# for this section */
|
||||
uint64_t s_offset; /* managed by elf_update() */
|
||||
uint64_t s_rawoff; /* original offset in the file */
|
||||
uint64_t s_size; /* managed by elf_update() */
|
||||
};
|
||||
|
||||
|
||||
enum {
|
||||
ELF_TOFILE,
|
||||
ELF_TOMEMORY
|
||||
ELF_TOFILE,
|
||||
ELF_TOMEMORY
|
||||
};
|
||||
|
||||
#define LIBELF_COPY_U32(DST,SRC,NAME) do { \
|
||||
if ((SRC)->NAME > UINT_MAX) { \
|
||||
LIBELF_SET_ERROR(RANGE, 0); \
|
||||
return (0); \
|
||||
} \
|
||||
(DST)->NAME = (SRC)->NAME; \
|
||||
} while (0)
|
||||
|
||||
#define LIBELF_COPY_S32(DST,SRC,NAME) do { \
|
||||
if ((SRC)->NAME > INT_MAX || \
|
||||
(SRC)->NAME < INT_MIN) { \
|
||||
LIBELF_SET_ERROR(RANGE, 0); \
|
||||
return (0); \
|
||||
} \
|
||||
(DST)->NAME = (SRC)->NAME; \
|
||||
} while (0)
|
||||
|
||||
|
||||
/*
|
||||
* Prototypes
|
||||
* The LIBELF_COPY macros are used to copy fields from a GElf_*
|
||||
* structure to their 32-bit counterparts, while checking for out of
|
||||
* range values.
|
||||
*
|
||||
* - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
|
||||
* - LIBELF_COPY_S32 :: copy a signed 32 bit field.
|
||||
*/
|
||||
|
||||
Elf_Data *_libelf_allocate_data(Elf_Scn *_s);
|
||||
#define LIBELF_COPY_U32(DST, SRC, NAME) do { \
|
||||
if ((SRC)->NAME > UINT32_MAX) { \
|
||||
LIBELF_SET_ERROR(RANGE, 0); \
|
||||
return (0); \
|
||||
} \
|
||||
(DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \
|
||||
} while (0)
|
||||
|
||||
#define LIBELF_COPY_S32(DST, SRC, NAME) do { \
|
||||
if ((SRC)->NAME > INT32_MAX || \
|
||||
(SRC)->NAME < INT32_MIN) { \
|
||||
LIBELF_SET_ERROR(RANGE, 0); \
|
||||
return (0); \
|
||||
} \
|
||||
(DST)->NAME = (int32_t) (SRC)->NAME; \
|
||||
} while (0)
|
||||
|
||||
|
||||
/*
|
||||
* Function Prototypes.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s);
|
||||
Elf *_libelf_allocate_elf(void);
|
||||
Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);
|
||||
Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
|
||||
Elf *_libelf_ar_open(Elf *_e);
|
||||
Elf *_libelf_ar_open(Elf *_e, int _reporterror);
|
||||
Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
|
||||
Elf_Arsym *_libelf_ar_process_symtab(Elf *_ar, size_t *_dst);
|
||||
unsigned long _libelf_checksum(Elf *_e, int _elfclass);
|
||||
Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
|
||||
Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
|
||||
long _libelf_checksum(Elf *_e, int _elfclass);
|
||||
void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
|
||||
int _libelf_falign(Elf_Type _t, int _elfclass);
|
||||
unsigned int _libelf_falign(Elf_Type _t, int _elfclass);
|
||||
size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
|
||||
size_t count);
|
||||
void (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
|
||||
(char *_dst, char *_src, size_t _cnt, int _byteswap);
|
||||
int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
|
||||
(unsigned char *_dst, size_t dsz, unsigned char *_src,
|
||||
size_t _cnt, int _byteswap);
|
||||
void *_libelf_getphdr(Elf *_e, int _elfclass);
|
||||
void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
|
||||
void _libelf_init_elf(Elf *_e, Elf_Kind _kind);
|
||||
int _libelf_malign(Elf_Type _t, int _elfclass);
|
||||
int _libelf_load_section_headers(Elf *e, void *ehdr);
|
||||
unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
|
||||
Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
|
||||
size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
|
||||
void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
|
||||
Elf_Data *_libelf_release_data(Elf_Data *_d);
|
||||
Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
|
||||
struct _Libelf_Data *_libelf_release_data(struct _Libelf_Data *_d);
|
||||
Elf *_libelf_release_elf(Elf *_e);
|
||||
Elf_Scn *_libelf_release_scn(Elf_Scn *_s);
|
||||
int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
|
||||
@@ -190,5 +232,8 @@ int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
|
||||
Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
|
||||
unsigned int _encoding, int _elfclass, int _direction);
|
||||
int _libelf_xlate_shtype(uint32_t _sht);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __LIBELF_H_ */
|
||||
|
||||
57
ext/libelf/_libelf_ar.h
Normal file
57
ext/libelf/_libelf_ar.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/*-
|
||||
* Copyright (c) 2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: _libelf_ar.h 3013 2014-03-23 06:16:59Z jkoshy $
|
||||
*/
|
||||
|
||||
#ifndef __LIBELF_AR_H_
|
||||
#define __LIBELF_AR_H_
|
||||
|
||||
/*
|
||||
* Prototypes and declarations needed by libelf's ar(1) archive
|
||||
* handling code.
|
||||
*/
|
||||
|
||||
#include <ar.h>
|
||||
|
||||
#define LIBELF_AR_BSD_EXTENDED_NAME_PREFIX "#1/"
|
||||
#define LIBELF_AR_BSD_SYMTAB_NAME "__.SYMDEF"
|
||||
#define LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE \
|
||||
(sizeof(LIBELF_AR_BSD_EXTENDED_NAME_PREFIX) - 1)
|
||||
|
||||
#define IS_EXTENDED_BSD_NAME(NAME) \
|
||||
(strncmp((const char *) (NAME), \
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX, \
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE) == 0)
|
||||
|
||||
|
||||
unsigned char *_libelf_ar_get_string(const char *_buf, size_t _sz,
|
||||
unsigned int _rawname, int _svr4names);
|
||||
char *_libelf_ar_get_raw_name(const struct ar_hdr *_arh);
|
||||
char *_libelf_ar_get_translated_name(const struct ar_hdr *_arh, Elf *_ar);
|
||||
int _libelf_ar_get_number(const char *_buf, size_t _sz,
|
||||
unsigned int _base, size_t *_ret);
|
||||
|
||||
#endif /* __LIBELF_AR_H_ */
|
||||
183
ext/libelf/_libelf_config.h
Normal file
183
ext/libelf/_libelf_config.h
Normal file
@@ -0,0 +1,183 @@
|
||||
/*-
|
||||
* Copyright (c) 2008-2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: _libelf_config.h 3168 2015-02-24 19:17:47Z emaste $
|
||||
*/
|
||||
|
||||
#if defined(__APPLE__) || defined(__DragonFly__)
|
||||
|
||||
#if defined(__amd64__)
|
||||
#define LIBELF_ARCH EM_X86_64
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS64
|
||||
#elif defined(__i386__)
|
||||
#define LIBELF_ARCH EM_386
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
#endif
|
||||
|
||||
#endif /* __DragonFly__ */
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
|
||||
/*
|
||||
* Define LIBELF_{ARCH,BYTEORDER,CLASS} based on the machine architecture.
|
||||
* See also: <machine/elf.h>.
|
||||
*/
|
||||
|
||||
#if defined(__amd64__)
|
||||
|
||||
#define LIBELF_ARCH EM_X86_64
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS64
|
||||
|
||||
#elif defined(__aarch64__)
|
||||
|
||||
#define LIBELF_ARCH EM_AARCH64
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS64
|
||||
|
||||
#elif defined(__arm__)
|
||||
|
||||
#define LIBELF_ARCH EM_ARM
|
||||
#if defined(__ARMEB__) /* Big-endian ARM. */
|
||||
#define LIBELF_BYTEORDER ELFDATA2MSB
|
||||
#else
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#endif
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
|
||||
#elif defined(__i386__)
|
||||
|
||||
#define LIBELF_ARCH EM_386
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
|
||||
#elif defined(__ia64__)
|
||||
|
||||
#define LIBELF_ARCH EM_IA_64
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS64
|
||||
|
||||
#elif defined(__mips__)
|
||||
|
||||
#define LIBELF_ARCH EM_MIPS
|
||||
#if defined(__MIPSEB__)
|
||||
#define LIBELF_BYTEORDER ELFDATA2MSB
|
||||
#else
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#endif
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
|
||||
#elif defined(__powerpc__)
|
||||
|
||||
#define LIBELF_ARCH EM_PPC
|
||||
#define LIBELF_BYTEORDER ELFDATA2MSB
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
|
||||
#elif defined(__sparc__)
|
||||
|
||||
#define LIBELF_ARCH EM_SPARCV9
|
||||
#define LIBELF_BYTEORDER ELFDATA2MSB
|
||||
#define LIBELF_CLASS ELFCLASS64
|
||||
|
||||
#else
|
||||
#error Unknown FreeBSD architecture.
|
||||
#endif
|
||||
#endif /* __FreeBSD__ */
|
||||
|
||||
/*
|
||||
* Definitions for Minix3.
|
||||
*/
|
||||
#ifdef __minix
|
||||
|
||||
#define LIBELF_ARCH EM_386
|
||||
#define LIBELF_BYTEORDER ELFDATA2LSB
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
|
||||
#endif /* __minix */
|
||||
|
||||
#ifdef __NetBSD__
|
||||
|
||||
#include <machine/elf_machdep.h>
|
||||
|
||||
#if !defined(ARCH_ELFSIZE)
|
||||
#error ARCH_ELFSIZE is not defined.
|
||||
#endif
|
||||
|
||||
#if ARCH_ELFSIZE == 32
|
||||
#define LIBELF_ARCH ELF32_MACHDEP_ID
|
||||
#define LIBELF_BYTEORDER ELF32_MACHDEP_ENDIANNESS
|
||||
#define LIBELF_CLASS ELFCLASS32
|
||||
#define Elf_Note Elf32_Nhdr
|
||||
#else
|
||||
#define LIBELF_ARCH ELF64_MACHDEP_ID
|
||||
#define LIBELF_BYTEORDER ELF64_MACHDEP_ENDIANNESS
|
||||
#define LIBELF_CLASS ELFCLASS64
|
||||
#define Elf_Note Elf64_Nhdr
|
||||
#endif
|
||||
|
||||
#endif /* __NetBSD__ */
|
||||
|
||||
#if defined(__OpenBSD__)
|
||||
|
||||
#include <machine/exec.h>
|
||||
|
||||
#define LIBELF_ARCH ELF_TARG_MACH
|
||||
#define LIBELF_BYTEORDER ELF_TARG_DATA
|
||||
#define LIBELF_CLASS ELF_TARG_CLASS
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* GNU & Linux compatibility.
|
||||
*
|
||||
* `__linux__' is defined in an environment runs the Linux kernel and glibc.
|
||||
* `__GNU__' is defined in an environment runs a GNU kernel (Hurd) and glibc.
|
||||
* `__GLIBC__' is defined for an environment that runs glibc over a non-GNU
|
||||
* kernel such as GNU/kFreeBSD.
|
||||
*/
|
||||
|
||||
#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
#include "native-elf-format.h"
|
||||
|
||||
#define LIBELF_CLASS ELFTC_CLASS
|
||||
#define LIBELF_ARCH ELFTC_ARCH
|
||||
#define LIBELF_BYTEORDER ELFTC_BYTEORDER
|
||||
|
||||
#endif /* defined(__linux__) */
|
||||
|
||||
#if LIBELF_CLASS == ELFCLASS32
|
||||
#define Elf_Note Elf32_Nhdr
|
||||
#elif LIBELF_CLASS == ELFCLASS64
|
||||
#define Elf_Note Elf64_Nhdr
|
||||
#else
|
||||
#error LIBELF_CLASS needs to be one of ELFCLASS32 or ELFCLASS64
|
||||
#endif
|
||||
|
||||
#endif /* defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) */
|
||||
40
ext/libelf/elf.c
Normal file
40
ext/libelf/elf.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/*-
|
||||
* Copyright (c) 2006,2008,2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf.c 2225 2011-11-26 18:55:54Z jkoshy $");
|
||||
|
||||
struct _libelf_globals _libelf = {
|
||||
.libelf_arch = LIBELF_ARCH,
|
||||
.libelf_byteorder = LIBELF_BYTEORDER,
|
||||
.libelf_class = LIBELF_CLASS,
|
||||
.libelf_error = 0,
|
||||
.libelf_fillchar = 0,
|
||||
.libelf_version = EV_NONE
|
||||
};
|
||||
@@ -1,245 +0,0 @@
|
||||
/*-
|
||||
* Copyright (c) 1996-1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf32.h,v 1.13 2006/10/17 05:43:30 jkoshy Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF32_H_
|
||||
#define _SYS_ELF32_H_ 1
|
||||
|
||||
#include "elf_common.h"
|
||||
|
||||
/*
|
||||
* ELF definitions common to all 32-bit architectures.
|
||||
*/
|
||||
|
||||
typedef uint32_t Elf32_Addr;
|
||||
typedef uint16_t Elf32_Half;
|
||||
typedef uint32_t Elf32_Off;
|
||||
typedef int32_t Elf32_Sword;
|
||||
typedef uint32_t Elf32_Word;
|
||||
typedef uint64_t Elf32_Lword;
|
||||
|
||||
typedef Elf32_Word Elf32_Hashelt;
|
||||
|
||||
/* Non-standard class-dependent datatype used for abstraction. */
|
||||
typedef Elf32_Word Elf32_Size;
|
||||
typedef Elf32_Sword Elf32_Ssize;
|
||||
|
||||
/*
|
||||
* ELF header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT]; /* File identification. */
|
||||
Elf32_Half e_type; /* File type. */
|
||||
Elf32_Half e_machine; /* Machine architecture. */
|
||||
Elf32_Word e_version; /* ELF format version. */
|
||||
Elf32_Addr e_entry; /* Entry point. */
|
||||
Elf32_Off e_phoff; /* Program header file offset. */
|
||||
Elf32_Off e_shoff; /* Section header file offset. */
|
||||
Elf32_Word e_flags; /* Architecture-specific flags. */
|
||||
Elf32_Half e_ehsize; /* Size of ELF header in bytes. */
|
||||
Elf32_Half e_phentsize; /* Size of program header entry. */
|
||||
Elf32_Half e_phnum; /* Number of program header entries. */
|
||||
Elf32_Half e_shentsize; /* Size of section header entry. */
|
||||
Elf32_Half e_shnum; /* Number of section header entries. */
|
||||
Elf32_Half e_shstrndx; /* Section name strings section. */
|
||||
} Elf32_Ehdr;
|
||||
|
||||
/*
|
||||
* Section header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Word sh_name; /* Section name (index into the
|
||||
section header string table). */
|
||||
Elf32_Word sh_type; /* Section type. */
|
||||
Elf32_Word sh_flags; /* Section flags. */
|
||||
Elf32_Addr sh_addr; /* Address in memory image. */
|
||||
Elf32_Off sh_offset; /* Offset in file. */
|
||||
Elf32_Word sh_size; /* Size in bytes. */
|
||||
Elf32_Word sh_link; /* Index of a related section. */
|
||||
Elf32_Word sh_info; /* Depends on section type. */
|
||||
Elf32_Word sh_addralign; /* Alignment in bytes. */
|
||||
Elf32_Word sh_entsize; /* Size of each entry in section. */
|
||||
} Elf32_Shdr;
|
||||
|
||||
/*
|
||||
* Program header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Word p_type; /* Entry type. */
|
||||
Elf32_Off p_offset; /* File offset of contents. */
|
||||
Elf32_Addr p_vaddr; /* Virtual address in memory image. */
|
||||
Elf32_Addr p_paddr; /* Physical address (not used). */
|
||||
Elf32_Word p_filesz; /* Size of contents in file. */
|
||||
Elf32_Word p_memsz; /* Size of contents in memory. */
|
||||
Elf32_Word p_flags; /* Access permission flags. */
|
||||
Elf32_Word p_align; /* Alignment in memory and file. */
|
||||
} Elf32_Phdr;
|
||||
|
||||
/*
|
||||
* Dynamic structure. The ".dynamic" section contains an array of them.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Sword d_tag; /* Entry type. */
|
||||
union {
|
||||
Elf32_Word d_val; /* Integer value. */
|
||||
Elf32_Addr d_ptr; /* Address value. */
|
||||
} d_un;
|
||||
} Elf32_Dyn;
|
||||
|
||||
/*
|
||||
* Relocation entries.
|
||||
*/
|
||||
|
||||
/* Relocations that don't need an addend field. */
|
||||
typedef struct {
|
||||
Elf32_Addr r_offset; /* Location to be relocated. */
|
||||
Elf32_Word r_info; /* Relocation type and symbol index. */
|
||||
} Elf32_Rel;
|
||||
|
||||
/* Relocations that need an addend field. */
|
||||
typedef struct {
|
||||
Elf32_Addr r_offset; /* Location to be relocated. */
|
||||
Elf32_Word r_info; /* Relocation type and symbol index. */
|
||||
Elf32_Sword r_addend; /* Addend. */
|
||||
} Elf32_Rela;
|
||||
|
||||
/* Macros for accessing the fields of r_info. */
|
||||
#define ELF32_R_SYM(info) ((info) >> 8)
|
||||
#define ELF32_R_TYPE(info) ((unsigned char)(info))
|
||||
|
||||
/* Macro for constructing r_info from field values. */
|
||||
#define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type))
|
||||
|
||||
/*
|
||||
* Note entry header
|
||||
*/
|
||||
typedef Elf_Note Elf32_Nhdr;
|
||||
|
||||
/*
|
||||
* Move entry
|
||||
*/
|
||||
typedef struct {
|
||||
Elf32_Lword m_value; /* symbol value */
|
||||
Elf32_Word m_info; /* size + index */
|
||||
Elf32_Word m_poffset; /* symbol offset */
|
||||
Elf32_Half m_repeat; /* repeat count */
|
||||
Elf32_Half m_stride; /* stride info */
|
||||
} Elf32_Move;
|
||||
|
||||
/*
|
||||
* The macros compose and decompose values for Move.r_info
|
||||
*
|
||||
* sym = ELF32_M_SYM(M.m_info)
|
||||
* size = ELF32_M_SIZE(M.m_info)
|
||||
* M.m_info = ELF32_M_INFO(sym, size)
|
||||
*/
|
||||
#define ELF32_M_SYM(info) ((info)>>8)
|
||||
#define ELF32_M_SIZE(info) ((unsigned char)(info))
|
||||
#define ELF32_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size))
|
||||
|
||||
/*
|
||||
* Hardware/Software capabilities entry
|
||||
*/
|
||||
typedef struct {
|
||||
Elf32_Word c_tag; /* how to interpret value */
|
||||
union {
|
||||
Elf32_Word c_val;
|
||||
Elf32_Addr c_ptr;
|
||||
} c_un;
|
||||
} Elf32_Cap;
|
||||
|
||||
/*
|
||||
* Symbol table entries.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf32_Word st_name; /* String table index of name. */
|
||||
Elf32_Addr st_value; /* Symbol value. */
|
||||
Elf32_Word st_size; /* Size of associated object. */
|
||||
unsigned char st_info; /* Type and binding information. */
|
||||
unsigned char st_other; /* Reserved (not used). */
|
||||
Elf32_Half st_shndx; /* Section index of symbol. */
|
||||
} Elf32_Sym;
|
||||
|
||||
/* Macros for accessing the fields of st_info. */
|
||||
#define ELF32_ST_BIND(info) ((info) >> 4)
|
||||
#define ELF32_ST_TYPE(info) ((info) & 0xf)
|
||||
|
||||
/* Macro for constructing st_info from field values. */
|
||||
#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
|
||||
|
||||
/* Macro for accessing the fields of st_other. */
|
||||
#define ELF32_ST_VISIBILITY(oth) ((oth) & 0x3)
|
||||
|
||||
/* Structures used by Sun & GNU symbol versioning. */
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Half vd_version;
|
||||
Elf32_Half vd_flags;
|
||||
Elf32_Half vd_ndx;
|
||||
Elf32_Half vd_cnt;
|
||||
Elf32_Word vd_hash;
|
||||
Elf32_Word vd_aux;
|
||||
Elf32_Word vd_next;
|
||||
} Elf32_Verdef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Word vda_name;
|
||||
Elf32_Word vda_next;
|
||||
} Elf32_Verdaux;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Half vn_version;
|
||||
Elf32_Half vn_cnt;
|
||||
Elf32_Word vn_file;
|
||||
Elf32_Word vn_aux;
|
||||
Elf32_Word vn_next;
|
||||
} Elf32_Verneed;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Elf32_Word vna_hash;
|
||||
Elf32_Half vna_flags;
|
||||
Elf32_Half vna_other;
|
||||
Elf32_Word vna_name;
|
||||
Elf32_Word vna_next;
|
||||
} Elf32_Vernaux;
|
||||
|
||||
typedef Elf32_Half Elf32_Versym;
|
||||
|
||||
typedef struct {
|
||||
Elf32_Half si_boundto; /* direct bindings - symbol bound to */
|
||||
Elf32_Half si_flags; /* per symbol flags */
|
||||
} Elf32_Syminfo;
|
||||
|
||||
#endif /* !_SYS_ELF32_H_ */
|
||||
@@ -1,248 +0,0 @@
|
||||
/*-
|
||||
* Copyright (c) 1996-1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf64.h,v 1.17 2006/10/17 05:43:30 jkoshy Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF64_H_
|
||||
#define _SYS_ELF64_H_ 1
|
||||
|
||||
#include "elf_common.h"
|
||||
|
||||
/*
|
||||
* ELF definitions common to all 64-bit architectures.
|
||||
*/
|
||||
|
||||
typedef uint64_t Elf64_Addr;
|
||||
typedef uint16_t Elf64_Half;
|
||||
typedef uint64_t Elf64_Off;
|
||||
typedef int32_t Elf64_Sword;
|
||||
typedef int64_t Elf64_Sxword;
|
||||
typedef uint32_t Elf64_Word;
|
||||
typedef uint64_t Elf64_Lword;
|
||||
typedef uint64_t Elf64_Xword;
|
||||
|
||||
/*
|
||||
* Types of dynamic symbol hash table bucket and chain elements.
|
||||
*
|
||||
* This is inconsistent among 64 bit architectures, so a machine dependent
|
||||
* typedef is required.
|
||||
*/
|
||||
|
||||
typedef Elf64_Word Elf64_Hashelt;
|
||||
|
||||
/* Non-standard class-dependent datatype used for abstraction. */
|
||||
typedef Elf64_Xword Elf64_Size;
|
||||
typedef Elf64_Sxword Elf64_Ssize;
|
||||
|
||||
/*
|
||||
* ELF header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT]; /* File identification. */
|
||||
Elf64_Half e_type; /* File type. */
|
||||
Elf64_Half e_machine; /* Machine architecture. */
|
||||
Elf64_Word e_version; /* ELF format version. */
|
||||
Elf64_Addr e_entry; /* Entry point. */
|
||||
Elf64_Off e_phoff; /* Program header file offset. */
|
||||
Elf64_Off e_shoff; /* Section header file offset. */
|
||||
Elf64_Word e_flags; /* Architecture-specific flags. */
|
||||
Elf64_Half e_ehsize; /* Size of ELF header in bytes. */
|
||||
Elf64_Half e_phentsize; /* Size of program header entry. */
|
||||
Elf64_Half e_phnum; /* Number of program header entries. */
|
||||
Elf64_Half e_shentsize; /* Size of section header entry. */
|
||||
Elf64_Half e_shnum; /* Number of section header entries. */
|
||||
Elf64_Half e_shstrndx; /* Section name strings section. */
|
||||
} Elf64_Ehdr;
|
||||
|
||||
/*
|
||||
* Section header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Word sh_name; /* Section name (index into the
|
||||
section header string table). */
|
||||
Elf64_Word sh_type; /* Section type. */
|
||||
Elf64_Xword sh_flags; /* Section flags. */
|
||||
Elf64_Addr sh_addr; /* Address in memory image. */
|
||||
Elf64_Off sh_offset; /* Offset in file. */
|
||||
Elf64_Xword sh_size; /* Size in bytes. */
|
||||
Elf64_Word sh_link; /* Index of a related section. */
|
||||
Elf64_Word sh_info; /* Depends on section type. */
|
||||
Elf64_Xword sh_addralign; /* Alignment in bytes. */
|
||||
Elf64_Xword sh_entsize; /* Size of each entry in section. */
|
||||
} Elf64_Shdr;
|
||||
|
||||
/*
|
||||
* Program header.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Word p_type; /* Entry type. */
|
||||
Elf64_Word p_flags; /* Access permission flags. */
|
||||
Elf64_Off p_offset; /* File offset of contents. */
|
||||
Elf64_Addr p_vaddr; /* Virtual address in memory image. */
|
||||
Elf64_Addr p_paddr; /* Physical address (not used). */
|
||||
Elf64_Xword p_filesz; /* Size of contents in file. */
|
||||
Elf64_Xword p_memsz; /* Size of contents in memory. */
|
||||
Elf64_Xword p_align; /* Alignment in memory and file. */
|
||||
} Elf64_Phdr;
|
||||
|
||||
/*
|
||||
* Dynamic structure. The ".dynamic" section contains an array of them.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Sxword d_tag; /* Entry type. */
|
||||
union {
|
||||
Elf64_Xword d_val; /* Integer value. */
|
||||
Elf64_Addr d_ptr; /* Address value. */
|
||||
} d_un;
|
||||
} Elf64_Dyn;
|
||||
|
||||
/*
|
||||
* Relocation entries.
|
||||
*/
|
||||
|
||||
/* Relocations that don't need an addend field. */
|
||||
typedef struct {
|
||||
Elf64_Addr r_offset; /* Location to be relocated. */
|
||||
Elf64_Xword r_info; /* Relocation type and symbol index. */
|
||||
} Elf64_Rel;
|
||||
|
||||
/* Relocations that need an addend field. */
|
||||
typedef struct {
|
||||
Elf64_Addr r_offset; /* Location to be relocated. */
|
||||
Elf64_Xword r_info; /* Relocation type and symbol index. */
|
||||
Elf64_Sxword r_addend; /* Addend. */
|
||||
} Elf64_Rela;
|
||||
|
||||
/* Macros for accessing the fields of r_info. */
|
||||
#define ELF64_R_SYM(info) ((info) >> 32)
|
||||
#define ELF64_R_TYPE(info) ((info) & 0xffffffffL)
|
||||
|
||||
/* Macro for constructing r_info from field values. */
|
||||
#define ELF64_R_INFO(sym, type) (((sym) << 32) + ((type) & 0xffffffffL))
|
||||
|
||||
#define ELF64_R_TYPE_DATA(info) (((Elf64_Xword)(info)<<32)>>40)
|
||||
#define ELF64_R_TYPE_ID(info) (((Elf64_Xword)(info)<<56)>>56)
|
||||
#define ELF64_R_TYPE_INFO(data, type) \
|
||||
(((Elf64_Xword)(data)<<8)+(Elf64_Xword)(type))
|
||||
|
||||
/*
|
||||
* Note entry header
|
||||
*/
|
||||
typedef Elf_Note Elf64_Nhdr;
|
||||
|
||||
/*
|
||||
* Move entry
|
||||
*/
|
||||
typedef struct {
|
||||
Elf64_Lword m_value; /* symbol value */
|
||||
Elf64_Xword m_info; /* size + index */
|
||||
Elf64_Xword m_poffset; /* symbol offset */
|
||||
Elf64_Half m_repeat; /* repeat count */
|
||||
Elf64_Half m_stride; /* stride info */
|
||||
} Elf64_Move;
|
||||
|
||||
#define ELF64_M_SYM(info) ((info)>>8)
|
||||
#define ELF64_M_SIZE(info) ((unsigned char)(info))
|
||||
#define ELF64_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size))
|
||||
|
||||
/*
|
||||
* Hardware/Software capabilities entry
|
||||
*/
|
||||
typedef struct {
|
||||
Elf64_Xword c_tag; /* how to interpret value */
|
||||
union {
|
||||
Elf64_Xword c_val;
|
||||
Elf64_Addr c_ptr;
|
||||
} c_un;
|
||||
} Elf64_Cap;
|
||||
|
||||
/*
|
||||
* Symbol table entries.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
Elf64_Word st_name; /* String table index of name. */
|
||||
unsigned char st_info; /* Type and binding information. */
|
||||
unsigned char st_other; /* Reserved (not used). */
|
||||
Elf64_Half st_shndx; /* Section index of symbol. */
|
||||
Elf64_Addr st_value; /* Symbol value. */
|
||||
Elf64_Xword st_size; /* Size of associated object. */
|
||||
} Elf64_Sym;
|
||||
|
||||
/* Macros for accessing the fields of st_info. */
|
||||
#define ELF64_ST_BIND(info) ((info) >> 4)
|
||||
#define ELF64_ST_TYPE(info) ((info) & 0xf)
|
||||
|
||||
/* Macro for constructing st_info from field values. */
|
||||
#define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
|
||||
|
||||
/* Macro for accessing the fields of st_other. */
|
||||
#define ELF64_ST_VISIBILITY(oth) ((oth) & 0x3)
|
||||
|
||||
/* Structures used by Sun & GNU-style symbol versioning. */
|
||||
typedef struct {
|
||||
Elf64_Half vd_version;
|
||||
Elf64_Half vd_flags;
|
||||
Elf64_Half vd_ndx;
|
||||
Elf64_Half vd_cnt;
|
||||
Elf64_Word vd_hash;
|
||||
Elf64_Word vd_aux;
|
||||
Elf64_Word vd_next;
|
||||
} Elf64_Verdef;
|
||||
|
||||
typedef struct {
|
||||
Elf64_Word vda_name;
|
||||
Elf64_Word vda_next;
|
||||
} Elf64_Verdaux;
|
||||
|
||||
typedef struct {
|
||||
Elf64_Half vn_version;
|
||||
Elf64_Half vn_cnt;
|
||||
Elf64_Word vn_file;
|
||||
Elf64_Word vn_aux;
|
||||
Elf64_Word vn_next;
|
||||
} Elf64_Verneed;
|
||||
|
||||
typedef struct {
|
||||
Elf64_Word vna_hash;
|
||||
Elf64_Half vna_flags;
|
||||
Elf64_Half vna_other;
|
||||
Elf64_Word vna_name;
|
||||
Elf64_Word vna_next;
|
||||
} Elf64_Vernaux;
|
||||
|
||||
typedef Elf64_Half Elf64_Versym;
|
||||
|
||||
typedef struct {
|
||||
Elf64_Half si_boundto; /* direct bindings - symbol bound to */
|
||||
Elf64_Half si_flags; /* per symbol flags */
|
||||
} Elf64_Syminfo;
|
||||
|
||||
#endif /* !_SYS_ELF64_H_ */
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,131 +24,68 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <ar.h>
|
||||
#include <ctype.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
static Elf *
|
||||
_libelf_open_object(int fd, Elf_Cmd c)
|
||||
{
|
||||
Elf *e;
|
||||
void *m;
|
||||
struct stat sb;
|
||||
|
||||
/*
|
||||
* 'Raw' files are always mapped with 'PROT_READ'. At
|
||||
* elf_update(3) time for files opened with ELF_C_RDWR the
|
||||
* mapping is unmapped, file data is written to using write(2)
|
||||
* and then the raw data is immediately mapped back in.
|
||||
*/
|
||||
if (fstat(fd, &sb) < 0) {
|
||||
LIBELF_SET_ERROR(IO, errno);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
m = NULL;
|
||||
if ((m = mmap(NULL, (size_t) sb.st_size, PROT_READ, MAP_PRIVATE, fd,
|
||||
(off_t) 0)) == MAP_FAILED) {
|
||||
LIBELF_SET_ERROR(IO, errno);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((e = elf_memory(m, (size_t) sb.st_size)) == NULL) {
|
||||
(void) munmap(m, (size_t) sb.st_size);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
e->e_flags |= LIBELF_F_MMAP;
|
||||
e->e_fd = fd;
|
||||
e->e_cmd = c;
|
||||
|
||||
if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
|
||||
(void) elf_end(e);
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (e);
|
||||
}
|
||||
ELFTC_VCSID("$Id: elf_begin.c 2364 2011-12-28 17:55:25Z jkoshy $");
|
||||
|
||||
Elf *
|
||||
elf_begin(int fd, Elf_Cmd c, Elf *a)
|
||||
{
|
||||
Elf *e;
|
||||
Elf *e;
|
||||
|
||||
e = NULL;
|
||||
e = NULL;
|
||||
|
||||
if (LIBELF_PRIVATE(version) == EV_NONE) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (LIBELF_PRIVATE(version) == EV_NONE) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case ELF_C_NULL:
|
||||
return (NULL);
|
||||
switch (c) {
|
||||
case ELF_C_NULL:
|
||||
return (NULL);
|
||||
|
||||
case ELF_C_WRITE:
|
||||
case ELF_C_WRITE:
|
||||
/*
|
||||
* The ELF_C_WRITE command is required to ignore the
|
||||
* descriptor passed in.
|
||||
*/
|
||||
a = NULL;
|
||||
break;
|
||||
|
||||
if (a != NULL) { /* not allowed for ar(1) archives. */
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
case ELF_C_RDWR:
|
||||
if (a != NULL) { /* not allowed for ar(1) archives. */
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
/*FALLTHROUGH*/
|
||||
case ELF_C_READ:
|
||||
/*
|
||||
* Descriptor `a' could be for a regular ELF file, or
|
||||
* for an ar(1) archive. If descriptor `a' was opened
|
||||
* using a valid file descriptor, we need to check if
|
||||
* the passed in `fd' value matches the original one.
|
||||
*/
|
||||
if (a &&
|
||||
((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
* Check writeability of `fd' immediately and fail if
|
||||
* not writeable.
|
||||
*/
|
||||
if (ftruncate(fd, (off_t) 0) < 0) {
|
||||
LIBELF_SET_ERROR(IO, errno);
|
||||
return (NULL);
|
||||
}
|
||||
default:
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
|
||||
if ((e = _libelf_allocate_elf()) != NULL) {
|
||||
_libelf_init_elf(e, ELF_K_ELF);
|
||||
e->e_byteorder = LIBELF_PRIVATE(byteorder);
|
||||
e->e_fd = fd;
|
||||
e->e_cmd = c;
|
||||
}
|
||||
return (e);
|
||||
}
|
||||
|
||||
case ELF_C_RDWR:
|
||||
if (a != NULL) { /* not allowed for ar(1) archives. */
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
/*FALLTHROUGH*/
|
||||
case ELF_C_READ:
|
||||
/*
|
||||
* Descriptor `a' could be for a regular ELF file, or
|
||||
* for an ar(1) archive.
|
||||
*/
|
||||
if (a && (a->e_fd != fd || c != a->e_cmd)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (a == NULL)
|
||||
e = _libelf_open_object(fd, c, 1);
|
||||
else if (a->e_kind == ELF_K_AR)
|
||||
e = _libelf_ar_open_member(a->e_fd, c, a);
|
||||
else
|
||||
(e = a)->e_activations++;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
|
||||
}
|
||||
|
||||
if (a == NULL)
|
||||
e = _libelf_open_object(fd, c);
|
||||
else if (a->e_kind == ELF_K_AR)
|
||||
e = _libelf_ar_open_member(fd, c, a);
|
||||
else
|
||||
(e = a)->e_activations++;
|
||||
|
||||
return (e);
|
||||
return (e);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,34 +24,35 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_cntl.c 2225 2011-11-26 18:55:54Z jkoshy $");
|
||||
|
||||
int
|
||||
elf_cntl(Elf *e, Elf_Cmd c)
|
||||
{
|
||||
if (e == NULL ||
|
||||
(c != ELF_C_FDDONE && c != ELF_C_FDREAD)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (-1);
|
||||
}
|
||||
if (e == NULL ||
|
||||
(c != ELF_C_FDDONE && c != ELF_C_FDREAD)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (e->e_parent) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (-1);
|
||||
}
|
||||
if (e->e_parent) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (c == ELF_C_FDREAD) {
|
||||
if (e->e_cmd == ELF_C_WRITE) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (-1);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
if (c == ELF_C_FDREAD) {
|
||||
if (e->e_cmd == ELF_C_WRITE) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (-1);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
e->e_fd = -1;
|
||||
return 0;
|
||||
e->e_fd = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,848 +0,0 @@
|
||||
/*-
|
||||
* Copyright (c) 1998 John D. Polstra.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/sys/sys/elf_common.h,v 1.22 2007/04/03 01:47:07 kan Exp $
|
||||
*/
|
||||
|
||||
#ifndef _SYS_ELF_COMMON_H_
|
||||
#define _SYS_ELF_COMMON_H_ 1
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
/*
|
||||
* ELF definitions that are independent of architecture or word size.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Note header. The ".note" section contains an array of notes. Each
|
||||
* begins with this header, aligned to a word boundary. Immediately
|
||||
* following the note header is n_namesz bytes of name, padded to the
|
||||
* next word boundary. Then comes n_descsz bytes of descriptor, again
|
||||
* padded to a word boundary. The values of n_namesz and n_descsz do
|
||||
* not include the padding.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
uint32_t n_namesz; /* Length of name. */
|
||||
uint32_t n_descsz; /* Length of descriptor. */
|
||||
uint32_t n_type; /* Type of this note. */
|
||||
} Elf_Note;
|
||||
|
||||
/* Indexes into the e_ident array. Keep synced with
|
||||
http://www.sco.com/developers/gabi/latest/ch4.eheader.html */
|
||||
#define EI_MAG0 0 /* Magic number, byte 0. */
|
||||
#define EI_MAG1 1 /* Magic number, byte 1. */
|
||||
#define EI_MAG2 2 /* Magic number, byte 2. */
|
||||
#define EI_MAG3 3 /* Magic number, byte 3. */
|
||||
#define EI_CLASS 4 /* Class of machine. */
|
||||
#define EI_DATA 5 /* Data format. */
|
||||
#define EI_VERSION 6 /* ELF format version. */
|
||||
#define EI_OSABI 7 /* Operating system / ABI identification */
|
||||
#define EI_ABIVERSION 8 /* ABI version */
|
||||
#define OLD_EI_BRAND 8 /* Start of architecture identification. */
|
||||
#define EI_PAD 9 /* Start of padding (per SVR4 ABI). */
|
||||
#define EI_NIDENT 16 /* Size of e_ident array. */
|
||||
|
||||
/* Values for the magic number bytes. */
|
||||
#define ELFMAG0 0x7f
|
||||
#define ELFMAG1 'E'
|
||||
#define ELFMAG2 'L'
|
||||
#define ELFMAG3 'F'
|
||||
#define ELFMAG "\177ELF" /* magic string */
|
||||
#define SELFMAG 4 /* magic string size */
|
||||
|
||||
/* Values for e_ident[EI_VERSION] and e_version. */
|
||||
#define EV_NONE 0
|
||||
#define EV_CURRENT 1
|
||||
|
||||
/* Values for e_ident[EI_CLASS]. */
|
||||
#define ELFCLASSNONE 0 /* Unknown class. */
|
||||
#define ELFCLASS32 1 /* 32-bit architecture. */
|
||||
#define ELFCLASS64 2 /* 64-bit architecture. */
|
||||
|
||||
/* Values for e_ident[EI_DATA]. */
|
||||
#define ELFDATANONE 0 /* Unknown data format. */
|
||||
#define ELFDATA2LSB 1 /* 2's complement little-endian. */
|
||||
#define ELFDATA2MSB 2 /* 2's complement big-endian. */
|
||||
|
||||
/* Values for e_ident[EI_OSABI]. */
|
||||
#define ELFOSABI_NONE 0 /* UNIX System V ABI */
|
||||
#define ELFOSABI_HPUX 1 /* HP-UX operating system */
|
||||
#define ELFOSABI_NETBSD 2 /* NetBSD */
|
||||
#define ELFOSABI_LINUX 3 /* GNU/Linux */
|
||||
#define ELFOSABI_HURD 4 /* GNU/Hurd */
|
||||
#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */
|
||||
#define ELFOSABI_SOLARIS 6 /* Solaris */
|
||||
#define ELFOSABI_AIX 7 /* AIX */
|
||||
#define ELFOSABI_IRIX 8 /* IRIX */
|
||||
#define ELFOSABI_FREEBSD 9 /* FreeBSD */
|
||||
#define ELFOSABI_TRU64 10 /* TRU64 UNIX */
|
||||
#define ELFOSABI_MODESTO 11 /* Novell Modesto */
|
||||
#define ELFOSABI_OPENBSD 12 /* OpenBSD */
|
||||
#define ELFOSABI_OPENVMS 13 /* Open VMS */
|
||||
#define ELFOSABI_NSK 14 /* HP Non-Stop Kernel */
|
||||
#define ELFOSABI_ARM 97 /* ARM */
|
||||
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
|
||||
|
||||
#define ELFOSABI_SYSV ELFOSABI_NONE /* symbol used in old spec */
|
||||
#define ELFOSABI_MONTEREY ELFOSABI_AIX /* Monterey */
|
||||
|
||||
/* e_ident */
|
||||
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
|
||||
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
|
||||
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
|
||||
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
|
||||
|
||||
/* Values for e_type. */
|
||||
#define ET_NONE 0 /* Unknown type. */
|
||||
#define ET_REL 1 /* Relocatable. */
|
||||
#define ET_EXEC 2 /* Executable. */
|
||||
#define ET_DYN 3 /* Shared object. */
|
||||
#define ET_CORE 4 /* Core file. */
|
||||
#define ET_LOOS 0xfe00 /* First operating system specific. */
|
||||
#define ET_HIOS 0xfeff /* Last operating system-specific. */
|
||||
#define ET_LOPROC 0xff00 /* First processor-specific. */
|
||||
#define ET_HIPROC 0xffff /* Last processor-specific. */
|
||||
|
||||
/* Values for e_machine. */
|
||||
#define EM_NONE 0 /* Unknown machine. */
|
||||
#define EM_M32 1 /* AT&T WE32100. */
|
||||
#define EM_SPARC 2 /* Sun SPARC. */
|
||||
#define EM_386 3 /* Intel i386. */
|
||||
#define EM_68K 4 /* Motorola 68000. */
|
||||
#define EM_88K 5 /* Motorola 88000. */
|
||||
#define EM_860 7 /* Intel i860. */
|
||||
#define EM_MIPS 8 /* MIPS R3000 Big-Endian only. */
|
||||
#define EM_S370 9 /* IBM System/370. */
|
||||
#define EM_MIPS_RS3_LE 10 /* MIPS R3000 Little-Endian. */
|
||||
#define EM_SPARC64 11 /* SPARC 64-bit */
|
||||
#define EM_PARISC 15 /* HP PA-RISC. */
|
||||
#define EM_VPP500 17 /* Fujitsu VPP500. */
|
||||
#define EM_SPARC32PLUS 18 /* SPARC v8plus. */
|
||||
#define EM_960 19 /* Intel 80960. */
|
||||
#define EM_PPC 20 /* PowerPC 32-bit. */
|
||||
#define EM_PPC64 21 /* PowerPC 64-bit. */
|
||||
#define EM_S390 22 /* IBM System/390. */
|
||||
#define EM_V800 36 /* NEC V800. */
|
||||
#define EM_FR20 37 /* Fujitsu FR20. */
|
||||
#define EM_RH32 38 /* TRW RH-32. */
|
||||
#define EM_RCE 39 /* Motorola RCE. */
|
||||
#define EM_ARM 40 /* ARM. */
|
||||
#define EM_SH 42 /* Hitachi SH. */
|
||||
#define EM_SPARCV9 43 /* SPARC v9 64-bit. */
|
||||
#define EM_TRICORE 44 /* Siemens TriCore embedded processor. */
|
||||
#define EM_ARC 45 /* Argonaut RISC Core. */
|
||||
#define EM_H8_300 46 /* Hitachi H8/300. */
|
||||
#define EM_H8_300H 47 /* Hitachi H8/300H. */
|
||||
#define EM_H8S 48 /* Hitachi H8S. */
|
||||
#define EM_H8_500 49 /* Hitachi H8/500. */
|
||||
#define EM_IA_64 50 /* Intel IA-64 Processor. */
|
||||
#define EM_MIPS_X 51 /* Stanford MIPS-X. */
|
||||
#define EM_COLDFIRE 52 /* Motorola ColdFire. */
|
||||
#define EM_68HC12 53 /* Motorola M68HC12. */
|
||||
#define EM_MMA 54 /* Fujitsu MMA. */
|
||||
#define EM_PCP 55 /* Siemens PCP. */
|
||||
#define EM_NCPU 56 /* Sony nCPU. */
|
||||
#define EM_NDR1 57 /* Denso NDR1 microprocessor. */
|
||||
#define EM_STARCORE 58 /* Motorola Star*Core processor. */
|
||||
#define EM_ME16 59 /* Toyota ME16 processor. */
|
||||
#define EM_ST100 60 /* STMicroelectronics ST100 processor. */
|
||||
#define EM_TINYJ 61 /* Advanced Logic Corp. TinyJ processor. */
|
||||
#define EM_X86_64 62 /* Advanced Micro Devices x86-64 */
|
||||
#define EM_AMD64 EM_X86_64 /* Advanced Micro Devices x86-64 (compat) */
|
||||
#define EM_AARCH64 183 /* AArch64 64 bit ARM. */
|
||||
#define EM_RISCV 243 /* Berkeley RISC-V */
|
||||
|
||||
/* Non-standard or deprecated. */
|
||||
#define EM_486 6 /* Intel i486. */
|
||||
#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
|
||||
#define EM_ALPHA_STD 41 /* Digital Alpha (standard value). */
|
||||
#define EM_ALPHA 0x9026 /* Alpha (written in the absence of an ABI) */
|
||||
|
||||
/* Special section indexes. */
|
||||
#define SHN_UNDEF 0 /* Undefined, missing, irrelevant. */
|
||||
#define SHN_LORESERVE 0xff00 /* First of reserved range. */
|
||||
#define SHN_LOPROC 0xff00 /* First processor-specific. */
|
||||
#define SHN_HIPROC 0xff1f /* Last processor-specific. */
|
||||
#define SHN_LOOS 0xff20 /* First operating system-specific. */
|
||||
#define SHN_HIOS 0xff3f /* Last operating system-specific. */
|
||||
#define SHN_ABS 0xfff1 /* Absolute values. */
|
||||
#define SHN_COMMON 0xfff2 /* Common data. */
|
||||
#define SHN_XINDEX 0xffff /* Escape -- index stored elsewhere. */
|
||||
#define SHN_HIRESERVE 0xffff /* Last of reserved range. */
|
||||
|
||||
/* sh_type */
|
||||
#define SHT_NULL 0 /* inactive */
|
||||
#define SHT_PROGBITS 1 /* program defined information */
|
||||
#define SHT_SYMTAB 2 /* symbol table section */
|
||||
#define SHT_STRTAB 3 /* string table section */
|
||||
#define SHT_RELA 4 /* relocation section with addends */
|
||||
#define SHT_HASH 5 /* symbol hash table section */
|
||||
#define SHT_DYNAMIC 6 /* dynamic section */
|
||||
#define SHT_NOTE 7 /* note section */
|
||||
#define SHT_NOBITS 8 /* no space section */
|
||||
#define SHT_REL 9 /* relocation section - no addends */
|
||||
#define SHT_SHLIB 10 /* reserved - purpose unknown */
|
||||
#define SHT_DYNSYM 11 /* dynamic symbol table section */
|
||||
#define SHT_INIT_ARRAY 14 /* Initialization function pointers. */
|
||||
#define SHT_FINI_ARRAY 15 /* Termination function pointers. */
|
||||
#define SHT_PREINIT_ARRAY 16 /* Pre-initialization function ptrs. */
|
||||
#define SHT_GROUP 17 /* Section group. */
|
||||
#define SHT_SYMTAB_SHNDX 18 /* Section indexes (see SHN_XINDEX). */
|
||||
#define SHT_LOOS 0x60000000 /* First of OS specific semantics */
|
||||
#define SHT_LOSUNW 0x6ffffff4
|
||||
#define SHT_SUNW_dof 0x6ffffff4
|
||||
#define SHT_SUNW_cap 0x6ffffff5
|
||||
#define SHT_SUNW_SIGNATURE 0x6ffffff6
|
||||
#define SHT_SUNW_ANNOTATE 0x6ffffff7
|
||||
#define SHT_SUNW_DEBUGSTR 0x6ffffff8
|
||||
#define SHT_SUNW_DEBUG 0x6ffffff9
|
||||
#define SHT_SUNW_move 0x6ffffffa
|
||||
#define SHT_SUNW_COMDAT 0x6ffffffb
|
||||
#define SHT_SUNW_syminfo 0x6ffffffc
|
||||
#define SHT_SUNW_verdef 0x6ffffffd
|
||||
#define SHT_GNU_verdef 0x6ffffffd /* Symbol versions provided */
|
||||
#define SHT_SUNW_verneed 0x6ffffffe
|
||||
#define SHT_GNU_verneed 0x6ffffffe /* Symbol versions required */
|
||||
#define SHT_SUNW_versym 0x6fffffff
|
||||
#define SHT_GNU_versym 0x6fffffff /* Symbol version table */
|
||||
#define SHT_HISUNW 0x6fffffff
|
||||
#define SHT_HIOS 0x6fffffff /* Last of OS specific semantics */
|
||||
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
|
||||
#define SHT_AMD64_UNWIND 0x70000001 /* unwind information */
|
||||
#define SHT_HIPROC 0x7fffffff /* specific section header types */
|
||||
#define SHT_LOUSER 0x80000000 /* reserved range for application */
|
||||
#define SHT_HIUSER 0xffffffff /* specific indexes */
|
||||
|
||||
/* Flags for sh_flags. */
|
||||
#define SHF_WRITE 0x1 /* Section contains writable data. */
|
||||
#define SHF_ALLOC 0x2 /* Section occupies memory. */
|
||||
#define SHF_EXECINSTR 0x4 /* Section contains instructions. */
|
||||
#define SHF_MERGE 0x10 /* Section may be merged. */
|
||||
#define SHF_STRINGS 0x20 /* Section contains strings. */
|
||||
#define SHF_INFO_LINK 0x40 /* sh_info holds section index. */
|
||||
#define SHF_LINK_ORDER 0x80 /* Special ordering requirements. */
|
||||
#define SHF_OS_NONCONFORMING 0x100 /* OS-specific processing required. */
|
||||
#define SHF_GROUP 0x200 /* Member of section group. */
|
||||
#define SHF_TLS 0x400 /* Section contains TLS data. */
|
||||
#define SHF_MASKOS 0x0ff00000 /* OS-specific semantics. */
|
||||
#define SHF_MASKPROC 0xf0000000 /* Processor-specific semantics. */
|
||||
|
||||
/* Values for p_type. */
|
||||
#define PT_NULL 0 /* Unused entry. */
|
||||
#define PT_LOAD 1 /* Loadable segment. */
|
||||
#define PT_DYNAMIC 2 /* Dynamic linking information segment. */
|
||||
#define PT_INTERP 3 /* Pathname of interpreter. */
|
||||
#define PT_NOTE 4 /* Auxiliary information. */
|
||||
#define PT_SHLIB 5 /* Reserved (not used). */
|
||||
#define PT_PHDR 6 /* Location of program header itself. */
|
||||
#define PT_TLS 7 /* Thread local storage segment */
|
||||
#define PT_LOOS 0x60000000 /* First OS-specific. */
|
||||
#define PT_SUNW_UNWIND 0x6464e550 /* amd64 UNWIND program header */
|
||||
#define PT_GNU_EH_FRAME 0x6474e550
|
||||
#define PT_LOSUNW 0x6ffffffa
|
||||
#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */
|
||||
#define PT_SUNWSTACK 0x6ffffffb /* describes the stack segment */
|
||||
#define PT_SUNWDTRACE 0x6ffffffc /* private */
|
||||
#define PT_SUNWCAP 0x6ffffffd /* hard/soft capabilities segment */
|
||||
#define PT_HISUNW 0x6fffffff
|
||||
#define PT_HIOS 0x6fffffff /* Last OS-specific. */
|
||||
#define PT_LOPROC 0x70000000 /* First processor-specific type. */
|
||||
#define PT_HIPROC 0x7fffffff /* Last processor-specific type. */
|
||||
|
||||
/* Values for p_flags. */
|
||||
#define PF_X 0x1 /* Executable. */
|
||||
#define PF_W 0x2 /* Writable. */
|
||||
#define PF_R 0x4 /* Readable. */
|
||||
#define PF_MASKOS 0x0ff00000 /* Operating system-specific. */
|
||||
#define PF_MASKPROC 0xf0000000 /* Processor-specific. */
|
||||
|
||||
/* Extended program header index. */
|
||||
#define PN_XNUM 0xffff
|
||||
|
||||
/* Values for d_tag. */
|
||||
#define DT_NULL 0 /* Terminating entry. */
|
||||
#define DT_NEEDED 1 /* String table offset of a needed shared
|
||||
library. */
|
||||
#define DT_PLTRELSZ 2 /* Total size in bytes of PLT relocations. */
|
||||
#define DT_PLTGOT 3 /* Processor-dependent address. */
|
||||
#define DT_HASH 4 /* Address of symbol hash table. */
|
||||
#define DT_STRTAB 5 /* Address of string table. */
|
||||
#define DT_SYMTAB 6 /* Address of symbol table. */
|
||||
#define DT_RELA 7 /* Address of ElfNN_Rela relocations. */
|
||||
#define DT_RELASZ 8 /* Total size of ElfNN_Rela relocations. */
|
||||
#define DT_RELAENT 9 /* Size of each ElfNN_Rela relocation entry. */
|
||||
#define DT_STRSZ 10 /* Size of string table. */
|
||||
#define DT_SYMENT 11 /* Size of each symbol table entry. */
|
||||
#define DT_INIT 12 /* Address of initialization function. */
|
||||
#define DT_FINI 13 /* Address of finalization function. */
|
||||
#define DT_SONAME 14 /* String table offset of shared object
|
||||
name. */
|
||||
#define DT_RPATH 15 /* String table offset of library path. [sup] */
|
||||
#define DT_SYMBOLIC 16 /* Indicates "symbolic" linking. [sup] */
|
||||
#define DT_REL 17 /* Address of ElfNN_Rel relocations. */
|
||||
#define DT_RELSZ 18 /* Total size of ElfNN_Rel relocations. */
|
||||
#define DT_RELENT 19 /* Size of each ElfNN_Rel relocation. */
|
||||
#define DT_PLTREL 20 /* Type of relocation used for PLT. */
|
||||
#define DT_DEBUG 21 /* Reserved (not used). */
|
||||
#define DT_TEXTREL 22 /* Indicates there may be relocations in
|
||||
non-writable segments. [sup] */
|
||||
#define DT_JMPREL 23 /* Address of PLT relocations. */
|
||||
#define DT_BIND_NOW 24 /* [sup] */
|
||||
#define DT_INIT_ARRAY 25 /* Address of the array of pointers to
|
||||
initialization functions */
|
||||
#define DT_FINI_ARRAY 26 /* Address of the array of pointers to
|
||||
termination functions */
|
||||
#define DT_INIT_ARRAYSZ 27 /* Size in bytes of the array of
|
||||
initialization functions. */
|
||||
#define DT_FINI_ARRAYSZ 28 /* Size in bytes of the array of
|
||||
terminationfunctions. */
|
||||
#define DT_RUNPATH 29 /* String table offset of a null-terminated
|
||||
library search path string. */
|
||||
#define DT_FLAGS 30 /* Object specific flag values. */
|
||||
#define DT_ENCODING 32 /* Values greater than or equal to DT_ENCODING
|
||||
and less than DT_LOOS follow the rules for
|
||||
the interpretation of the d_un union
|
||||
as follows: even == 'd_ptr', even == 'd_val'
|
||||
or none */
|
||||
#define DT_PREINIT_ARRAY 32 /* Address of the array of pointers to
|
||||
pre-initialization functions. */
|
||||
#define DT_PREINIT_ARRAYSZ 33 /* Size in bytes of the array of
|
||||
pre-initialization functions. */
|
||||
#define DT_MAXPOSTAGS 34 /* number of positive tags */
|
||||
#define DT_LOOS 0x6000000d /* First OS-specific */
|
||||
#define DT_SUNW_AUXILIARY 0x6000000d /* symbol auxiliary name */
|
||||
#define DT_SUNW_RTLDINF 0x6000000e /* ld.so.1 info (private) */
|
||||
#define DT_SUNW_FILTER 0x6000000f /* symbol filter name */
|
||||
#define DT_SUNW_CAP 0x60000010 /* hardware/software */
|
||||
#define DT_HIOS 0x6ffff000 /* Last OS-specific */
|
||||
|
||||
/*
|
||||
* DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the
|
||||
* Dyn.d_un.d_val field of the Elf*_Dyn structure.
|
||||
*/
|
||||
#define DT_VALRNGLO 0x6ffffd00
|
||||
#define DT_CHECKSUM 0x6ffffdf8 /* elf checksum */
|
||||
#define DT_PLTPADSZ 0x6ffffdf9 /* pltpadding size */
|
||||
#define DT_MOVEENT 0x6ffffdfa /* move table entry size */
|
||||
#define DT_MOVESZ 0x6ffffdfb /* move table size */
|
||||
#define DT_FEATURE_1 0x6ffffdfc /* feature holder */
|
||||
#define DT_POSFLAG_1 0x6ffffdfd /* flags for DT_* entries, effecting */
|
||||
/* the following DT_* entry. */
|
||||
/* See DF_P1_* definitions */
|
||||
#define DT_SYMINSZ 0x6ffffdfe /* syminfo table size (in bytes) */
|
||||
#define DT_SYMINENT 0x6ffffdff /* syminfo entry size (in bytes) */
|
||||
#define DT_VALRNGHI 0x6ffffdff
|
||||
|
||||
/*
|
||||
* DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the
|
||||
* Dyn.d_un.d_ptr field of the Elf*_Dyn structure.
|
||||
*
|
||||
* If any adjustment is made to the ELF object after it has been
|
||||
* built, these entries will need to be adjusted.
|
||||
*/
|
||||
#define DT_ADDRRNGLO 0x6ffffe00
|
||||
#define DT_CONFIG 0x6ffffefa /* configuration information */
|
||||
#define DT_DEPAUDIT 0x6ffffefb /* dependency auditing */
|
||||
#define DT_AUDIT 0x6ffffefc /* object auditing */
|
||||
#define DT_PLTPAD 0x6ffffefd /* pltpadding (sparcv9) */
|
||||
#define DT_MOVETAB 0x6ffffefe /* move table */
|
||||
#define DT_SYMINFO 0x6ffffeff /* syminfo table */
|
||||
#define DT_ADDRRNGHI 0x6ffffeff
|
||||
|
||||
#define DT_VERSYM 0x6ffffff0 /* Address of versym section. */
|
||||
#define DT_RELACOUNT 0x6ffffff9 /* number of RELATIVE relocations */
|
||||
#define DT_RELCOUNT 0x6ffffffa /* number of RELATIVE relocations */
|
||||
#define DT_FLAGS_1 0x6ffffffb /* state flags - see DF_1_* defs */
|
||||
#define DT_VERDEF 0x6ffffffc /* Address of verdef section. */
|
||||
#define DT_VERDEFNUM 0x6ffffffd /* Number of elems in verdef section */
|
||||
#define DT_VERNEED 0x6ffffffe /* Address of verneed section. */
|
||||
#define DT_VERNEEDNUM 0x6fffffff /* Number of elems in verneed section */
|
||||
|
||||
#define DT_LOPROC 0x70000000 /* First processor-specific type. */
|
||||
#define DT_DEPRECATED_SPARC_REGISTER 0x7000001
|
||||
#define DT_AUXILIARY 0x7ffffffd /* shared library auxiliary name */
|
||||
#define DT_USED 0x7ffffffe /* ignored - same as needed */
|
||||
#define DT_FILTER 0x7fffffff /* shared library filter name */
|
||||
#define DT_HIPROC 0x7fffffff /* Last processor-specific type. */
|
||||
|
||||
/* Values for DT_FLAGS */
|
||||
#define DF_ORIGIN 0x0001 /* Indicates that the object being loaded may
|
||||
make reference to the $ORIGIN substitution
|
||||
string */
|
||||
#define DF_SYMBOLIC 0x0002 /* Indicates "symbolic" linking. */
|
||||
#define DF_TEXTREL 0x0004 /* Indicates there may be relocations in
|
||||
non-writable segments. */
|
||||
#define DF_BIND_NOW 0x0008 /* Indicates that the dynamic linker should
|
||||
process all relocations for the object
|
||||
containing this entry before transferring
|
||||
control to the program. */
|
||||
#define DF_STATIC_TLS 0x0010 /* Indicates that the shared object or
|
||||
executable contains code using a static
|
||||
thread-local storage scheme. */
|
||||
|
||||
/* Values for n_type. Used in core files. */
|
||||
#define NT_PRSTATUS 1 /* Process status. */
|
||||
#define NT_FPREGSET 2 /* Floating point registers. */
|
||||
#define NT_PRPSINFO 3 /* Process state info. */
|
||||
|
||||
/* Symbol Binding - ELFNN_ST_BIND - st_info */
|
||||
#define STB_LOCAL 0 /* Local symbol */
|
||||
#define STB_GLOBAL 1 /* Global symbol */
|
||||
#define STB_WEAK 2 /* like global - lower precedence */
|
||||
#define STB_LOOS 10 /* Reserved range for operating system */
|
||||
#define STB_HIOS 12 /* specific semantics. */
|
||||
#define STB_LOPROC 13 /* reserved range for processor */
|
||||
#define STB_HIPROC 15 /* specific semantics. */
|
||||
|
||||
/* Symbol type - ELFNN_ST_TYPE - st_info */
|
||||
#define STT_NOTYPE 0 /* Unspecified type. */
|
||||
#define STT_OBJECT 1 /* Data object. */
|
||||
#define STT_FUNC 2 /* Function. */
|
||||
#define STT_SECTION 3 /* Section. */
|
||||
#define STT_FILE 4 /* Source file. */
|
||||
#define STT_COMMON 5 /* Uninitialized common block. */
|
||||
#define STT_TLS 6 /* TLS object. */
|
||||
#define STT_NUM 7
|
||||
#define STT_LOOS 10 /* Reserved range for operating system */
|
||||
#define STT_HIOS 12 /* specific semantics. */
|
||||
#define STT_LOPROC 13 /* reserved range for processor */
|
||||
#define STT_HIPROC 15 /* specific semantics. */
|
||||
|
||||
/* Symbol visibility - ELFNN_ST_VISIBILITY - st_other */
|
||||
#define STV_DEFAULT 0x0 /* Default visibility (see binding). */
|
||||
#define STV_INTERNAL 0x1 /* Special meaning in relocatable objects. */
|
||||
#define STV_HIDDEN 0x2 /* Not visible. */
|
||||
#define STV_PROTECTED 0x3 /* Visible but not preemptible. */
|
||||
|
||||
/* Special symbol table indexes. */
|
||||
#define STN_UNDEF 0 /* Undefined symbol index. */
|
||||
|
||||
/* Symbol versioning flags. */
|
||||
#define VER_DEF_CURRENT 1
|
||||
#define VER_DEF_IDX(x) VER_NDX(x)
|
||||
|
||||
#define VER_FLG_BASE 0x01
|
||||
#define VER_FLG_WEAK 0x02
|
||||
|
||||
#define VER_NEED_CURRENT 1
|
||||
#define VER_NEED_WEAK (1u << 15)
|
||||
#define VER_NEED_HIDDEN VER_NDX_HIDDEN
|
||||
#define VER_NEED_IDX(x) VER_NDX(x)
|
||||
|
||||
#define VER_NDX_LOCAL 0
|
||||
#define VER_NDX_GLOBAL 1
|
||||
#define VER_NDX_GIVEN 2
|
||||
|
||||
#define VER_NDX_HIDDEN (1u << 15)
|
||||
#define VER_NDX(x) ((x) & ~(1u << 15))
|
||||
|
||||
#define CA_SUNW_NULL 0
|
||||
#define CA_SUNW_HW_1 1 /* first hardware capabilities entry */
|
||||
#define CA_SUNW_SF_1 2 /* first software capabilities entry */
|
||||
|
||||
/*
|
||||
* Syminfo flag values
|
||||
*/
|
||||
#define SYMINFO_FLG_DIRECT 0x0001 /* symbol ref has direct association */
|
||||
/* to object containing defn. */
|
||||
#define SYMINFO_FLG_PASSTHRU 0x0002 /* ignored - see SYMINFO_FLG_FILTER */
|
||||
#define SYMINFO_FLG_COPY 0x0004 /* symbol is a copy-reloc */
|
||||
#define SYMINFO_FLG_LAZYLOAD 0x0008 /* object containing defn should be */
|
||||
/* lazily-loaded */
|
||||
#define SYMINFO_FLG_DIRECTBIND 0x0010 /* ref should be bound directly to */
|
||||
/* object containing defn. */
|
||||
#define SYMINFO_FLG_NOEXTDIRECT 0x0020 /* don't let an external reference */
|
||||
/* directly bind to this symbol */
|
||||
#define SYMINFO_FLG_FILTER 0x0002 /* symbol ref is associated to a */
|
||||
#define SYMINFO_FLG_AUXILIARY 0x0040 /* standard or auxiliary filter */
|
||||
|
||||
/*
|
||||
* Syminfo.si_boundto values.
|
||||
*/
|
||||
#define SYMINFO_BT_SELF 0xffff /* symbol bound to self */
|
||||
#define SYMINFO_BT_PARENT 0xfffe /* symbol bound to parent */
|
||||
#define SYMINFO_BT_NONE 0xfffd /* no special symbol binding */
|
||||
#define SYMINFO_BT_EXTERN 0xfffc /* symbol defined as external */
|
||||
#define SYMINFO_BT_LOWRESERVE 0xff00 /* beginning of reserved entries */
|
||||
|
||||
/*
|
||||
* Syminfo version values.
|
||||
*/
|
||||
#define SYMINFO_NONE 0 /* Syminfo version */
|
||||
#define SYMINFO_CURRENT 1
|
||||
#define SYMINFO_NUM 2
|
||||
|
||||
/*
|
||||
* Relocation types.
|
||||
*
|
||||
* All machine architectures are defined here to allow tools on one to
|
||||
* handle others.
|
||||
*/
|
||||
|
||||
#define R_386_NONE 0 /* No relocation. */
|
||||
#define R_386_32 1 /* Add symbol value. */
|
||||
#define R_386_PC32 2 /* Add PC-relative symbol value. */
|
||||
#define R_386_GOT32 3 /* Add PC-relative GOT offset. */
|
||||
#define R_386_PLT32 4 /* Add PC-relative PLT offset. */
|
||||
#define R_386_COPY 5 /* Copy data from shared object. */
|
||||
#define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */
|
||||
#define R_386_JMP_SLOT 7 /* Set GOT entry to code address. */
|
||||
#define R_386_RELATIVE 8 /* Add load address of shared object. */
|
||||
#define R_386_GOTOFF 9 /* Add GOT-relative symbol address. */
|
||||
#define R_386_GOTPC 10 /* Add PC-relative GOT table address. */
|
||||
#define R_386_TLS_TPOFF 14 /* Negative offset in static TLS block */
|
||||
#define R_386_TLS_IE 15 /* Absolute address of GOT for -ve static TLS */
|
||||
#define R_386_TLS_GOTIE 16 /* GOT entry for negative static TLS block */
|
||||
#define R_386_TLS_LE 17 /* Negative offset relative to static TLS */
|
||||
#define R_386_TLS_GD 18 /* 32 bit offset to GOT (index,off) pair */
|
||||
#define R_386_TLS_LDM 19 /* 32 bit offset to GOT (index,zero) pair */
|
||||
#define R_386_TLS_GD_32 24 /* 32 bit offset to GOT (index,off) pair */
|
||||
#define R_386_TLS_GD_PUSH 25 /* pushl instruction for Sun ABI GD sequence */
|
||||
#define R_386_TLS_GD_CALL 26 /* call instruction for Sun ABI GD sequence */
|
||||
#define R_386_TLS_GD_POP 27 /* popl instruction for Sun ABI GD sequence */
|
||||
#define R_386_TLS_LDM_32 28 /* 32 bit offset to GOT (index,zero) pair */
|
||||
#define R_386_TLS_LDM_PUSH 29 /* pushl instruction for Sun ABI LD sequence */
|
||||
#define R_386_TLS_LDM_CALL 30 /* call instruction for Sun ABI LD sequence */
|
||||
#define R_386_TLS_LDM_POP 31 /* popl instruction for Sun ABI LD sequence */
|
||||
#define R_386_TLS_LDO_32 32 /* 32 bit offset from start of TLS block */
|
||||
#define R_386_TLS_IE_32 33 /* 32 bit offset to GOT static TLS offset entry */
|
||||
#define R_386_TLS_LE_32 34 /* 32 bit offset within static TLS block */
|
||||
#define R_386_TLS_DTPMOD32 35 /* GOT entry containing TLS index */
|
||||
#define R_386_TLS_DTPOFF32 36 /* GOT entry containing TLS offset */
|
||||
#define R_386_TLS_TPOFF32 37 /* GOT entry of -ve static TLS offset */
|
||||
|
||||
#define R_ARM_NONE 0 /* No relocation. */
|
||||
#define R_ARM_PC24 1
|
||||
#define R_ARM_ABS32 2
|
||||
#define R_ARM_REL32 3
|
||||
#define R_ARM_PC13 4
|
||||
#define R_ARM_ABS16 5
|
||||
#define R_ARM_ABS12 6
|
||||
#define R_ARM_THM_ABS5 7
|
||||
#define R_ARM_ABS8 8
|
||||
#define R_ARM_SBREL32 9
|
||||
#define R_ARM_THM_PC22 10
|
||||
#define R_ARM_THM_PC8 11
|
||||
#define R_ARM_AMP_VCALL9 12
|
||||
#define R_ARM_SWI24 13
|
||||
#define R_ARM_THM_SWI8 14
|
||||
#define R_ARM_XPC25 15
|
||||
#define R_ARM_THM_XPC22 16
|
||||
#define R_ARM_COPY 20 /* Copy data from shared object. */
|
||||
#define R_ARM_GLOB_DAT 21 /* Set GOT entry to data address. */
|
||||
#define R_ARM_JUMP_SLOT 22 /* Set GOT entry to code address. */
|
||||
#define R_ARM_RELATIVE 23 /* Add load address of shared object. */
|
||||
#define R_ARM_GOTOFF 24 /* Add GOT-relative symbol address. */
|
||||
#define R_ARM_GOTPC 25 /* Add PC-relative GOT table address. */
|
||||
#define R_ARM_GOT32 26 /* Add PC-relative GOT offset. */
|
||||
#define R_ARM_PLT32 27 /* Add PC-relative PLT offset. */
|
||||
#define R_ARM_GNU_VTENTRY 100
|
||||
#define R_ARM_GNU_VTINHERIT 101
|
||||
#define R_ARM_RSBREL32 250
|
||||
#define R_ARM_THM_RPC22 251
|
||||
#define R_ARM_RREL32 252
|
||||
#define R_ARM_RABS32 253
|
||||
#define R_ARM_RPC24 254
|
||||
#define R_ARM_RBASE 255
|
||||
|
||||
/* Name Value Field Calculation */
|
||||
#define R_IA_64_NONE 0 /* None */
|
||||
#define R_IA_64_IMM14 0x21 /* immediate14 S + A */
|
||||
#define R_IA_64_IMM22 0x22 /* immediate22 S + A */
|
||||
#define R_IA_64_IMM64 0x23 /* immediate64 S + A */
|
||||
#define R_IA_64_DIR32MSB 0x24 /* word32 MSB S + A */
|
||||
#define R_IA_64_DIR32LSB 0x25 /* word32 LSB S + A */
|
||||
#define R_IA_64_DIR64MSB 0x26 /* word64 MSB S + A */
|
||||
#define R_IA_64_DIR64LSB 0x27 /* word64 LSB S + A */
|
||||
#define R_IA_64_GPREL22 0x2a /* immediate22 @gprel(S + A) */
|
||||
#define R_IA_64_GPREL64I 0x2b /* immediate64 @gprel(S + A) */
|
||||
#define R_IA_64_GPREL32MSB 0x2c /* word32 MSB @gprel(S + A) */
|
||||
#define R_IA_64_GPREL32LSB 0x2d /* word32 LSB @gprel(S + A) */
|
||||
#define R_IA_64_GPREL64MSB 0x2e /* word64 MSB @gprel(S + A) */
|
||||
#define R_IA_64_GPREL64LSB 0x2f /* word64 LSB @gprel(S + A) */
|
||||
#define R_IA_64_LTOFF22 0x32 /* immediate22 @ltoff(S + A) */
|
||||
#define R_IA_64_LTOFF64I 0x33 /* immediate64 @ltoff(S + A) */
|
||||
#define R_IA_64_PLTOFF22 0x3a /* immediate22 @pltoff(S + A) */
|
||||
#define R_IA_64_PLTOFF64I 0x3b /* immediate64 @pltoff(S + A) */
|
||||
#define R_IA_64_PLTOFF64MSB 0x3e /* word64 MSB @pltoff(S + A) */
|
||||
#define R_IA_64_PLTOFF64LSB 0x3f /* word64 LSB @pltoff(S + A) */
|
||||
#define R_IA_64_FPTR64I 0x43 /* immediate64 @fptr(S + A) */
|
||||
#define R_IA_64_FPTR32MSB 0x44 /* word32 MSB @fptr(S + A) */
|
||||
#define R_IA_64_FPTR32LSB 0x45 /* word32 LSB @fptr(S + A) */
|
||||
#define R_IA_64_FPTR64MSB 0x46 /* word64 MSB @fptr(S + A) */
|
||||
#define R_IA_64_FPTR64LSB 0x47 /* word64 LSB @fptr(S + A) */
|
||||
#define R_IA_64_PCREL60B 0x48 /* immediate60 form1 S + A - P */
|
||||
#define R_IA_64_PCREL21B 0x49 /* immediate21 form1 S + A - P */
|
||||
#define R_IA_64_PCREL21M 0x4a /* immediate21 form2 S + A - P */
|
||||
#define R_IA_64_PCREL21F 0x4b /* immediate21 form3 S + A - P */
|
||||
#define R_IA_64_PCREL32MSB 0x4c /* word32 MSB S + A - P */
|
||||
#define R_IA_64_PCREL32LSB 0x4d /* word32 LSB S + A - P */
|
||||
#define R_IA_64_PCREL64MSB 0x4e /* word64 MSB S + A - P */
|
||||
#define R_IA_64_PCREL64LSB 0x4f /* word64 LSB S + A - P */
|
||||
#define R_IA_64_LTOFF_FPTR22 0x52 /* immediate22 @ltoff(@fptr(S + A)) */
|
||||
#define R_IA_64_LTOFF_FPTR64I 0x53 /* immediate64 @ltoff(@fptr(S + A)) */
|
||||
#define R_IA_64_LTOFF_FPTR32MSB 0x54 /* word32 MSB @ltoff(@fptr(S + A)) */
|
||||
#define R_IA_64_LTOFF_FPTR32LSB 0x55 /* word32 LSB @ltoff(@fptr(S + A)) */
|
||||
#define R_IA_64_LTOFF_FPTR64MSB 0x56 /* word64 MSB @ltoff(@fptr(S + A)) */
|
||||
#define R_IA_64_LTOFF_FPTR64LSB 0x57 /* word64 LSB @ltoff(@fptr(S + A)) */
|
||||
#define R_IA_64_SEGREL32MSB 0x5c /* word32 MSB @segrel(S + A) */
|
||||
#define R_IA_64_SEGREL32LSB 0x5d /* word32 LSB @segrel(S + A) */
|
||||
#define R_IA_64_SEGREL64MSB 0x5e /* word64 MSB @segrel(S + A) */
|
||||
#define R_IA_64_SEGREL64LSB 0x5f /* word64 LSB @segrel(S + A) */
|
||||
#define R_IA_64_SECREL32MSB 0x64 /* word32 MSB @secrel(S + A) */
|
||||
#define R_IA_64_SECREL32LSB 0x65 /* word32 LSB @secrel(S + A) */
|
||||
#define R_IA_64_SECREL64MSB 0x66 /* word64 MSB @secrel(S + A) */
|
||||
#define R_IA_64_SECREL64LSB 0x67 /* word64 LSB @secrel(S + A) */
|
||||
#define R_IA_64_REL32MSB 0x6c /* word32 MSB BD + A */
|
||||
#define R_IA_64_REL32LSB 0x6d /* word32 LSB BD + A */
|
||||
#define R_IA_64_REL64MSB 0x6e /* word64 MSB BD + A */
|
||||
#define R_IA_64_REL64LSB 0x6f /* word64 LSB BD + A */
|
||||
#define R_IA_64_LTV32MSB 0x74 /* word32 MSB S + A */
|
||||
#define R_IA_64_LTV32LSB 0x75 /* word32 LSB S + A */
|
||||
#define R_IA_64_LTV64MSB 0x76 /* word64 MSB S + A */
|
||||
#define R_IA_64_LTV64LSB 0x77 /* word64 LSB S + A */
|
||||
#define R_IA_64_PCREL21BI 0x79 /* immediate21 form1 S + A - P */
|
||||
#define R_IA_64_PCREL22 0x7a /* immediate22 S + A - P */
|
||||
#define R_IA_64_PCREL64I 0x7b /* immediate64 S + A - P */
|
||||
#define R_IA_64_IPLTMSB 0x80 /* function descriptor MSB special */
|
||||
#define R_IA_64_IPLTLSB 0x81 /* function descriptor LSB speciaal */
|
||||
#define R_IA_64_SUB 0x85 /* immediate64 A - S */
|
||||
#define R_IA_64_LTOFF22X 0x86 /* immediate22 special */
|
||||
#define R_IA_64_LDXMOV 0x87 /* immediate22 special */
|
||||
#define R_IA_64_TPREL14 0x91 /* imm14 @tprel(S + A) */
|
||||
#define R_IA_64_TPREL22 0x92 /* imm22 @tprel(S + A) */
|
||||
#define R_IA_64_TPREL64I 0x93 /* imm64 @tprel(S + A) */
|
||||
#define R_IA_64_TPREL64MSB 0x96 /* word64 MSB @tprel(S + A) */
|
||||
#define R_IA_64_TPREL64LSB 0x97 /* word64 LSB @tprel(S + A) */
|
||||
#define R_IA_64_LTOFF_TPREL22 0x9a /* imm22 @ltoff(@tprel(S+A)) */
|
||||
#define R_IA_64_DTPMOD64MSB 0xa6 /* word64 MSB @dtpmod(S + A) */
|
||||
#define R_IA_64_DTPMOD64LSB 0xa7 /* word64 LSB @dtpmod(S + A) */
|
||||
#define R_IA_64_LTOFF_DTPMOD22 0xaa /* imm22 @ltoff(@dtpmod(S+A)) */
|
||||
#define R_IA_64_DTPREL14 0xb1 /* imm14 @dtprel(S + A) */
|
||||
#define R_IA_64_DTPREL22 0xb2 /* imm22 @dtprel(S + A) */
|
||||
#define R_IA_64_DTPREL64I 0xb3 /* imm64 @dtprel(S + A) */
|
||||
#define R_IA_64_DTPREL32MSB 0xb4 /* word32 MSB @dtprel(S + A) */
|
||||
#define R_IA_64_DTPREL32LSB 0xb5 /* word32 LSB @dtprel(S + A) */
|
||||
#define R_IA_64_DTPREL64MSB 0xb6 /* word64 MSB @dtprel(S + A) */
|
||||
#define R_IA_64_DTPREL64LSB 0xb7 /* word64 LSB @dtprel(S + A) */
|
||||
#define R_IA_64_LTOFF_DTPREL22 0xba /* imm22 @ltoff(@dtprel(S+A)) */
|
||||
|
||||
#define R_PPC_NONE 0 /* No relocation. */
|
||||
#define R_PPC_ADDR32 1
|
||||
#define R_PPC_ADDR24 2
|
||||
#define R_PPC_ADDR16 3
|
||||
#define R_PPC_ADDR16_LO 4
|
||||
#define R_PPC_ADDR16_HI 5
|
||||
#define R_PPC_ADDR16_HA 6
|
||||
#define R_PPC_ADDR14 7
|
||||
#define R_PPC_ADDR14_BRTAKEN 8
|
||||
#define R_PPC_ADDR14_BRNTAKEN 9
|
||||
#define R_PPC_REL24 10
|
||||
#define R_PPC_REL14 11
|
||||
#define R_PPC_REL14_BRTAKEN 12
|
||||
#define R_PPC_REL14_BRNTAKEN 13
|
||||
#define R_PPC_GOT16 14
|
||||
#define R_PPC_GOT16_LO 15
|
||||
#define R_PPC_GOT16_HI 16
|
||||
#define R_PPC_GOT16_HA 17
|
||||
#define R_PPC_PLTREL24 18
|
||||
#define R_PPC_COPY 19
|
||||
#define R_PPC_GLOB_DAT 20
|
||||
#define R_PPC_JMP_SLOT 21
|
||||
#define R_PPC_RELATIVE 22
|
||||
#define R_PPC_LOCAL24PC 23
|
||||
#define R_PPC_UADDR32 24
|
||||
#define R_PPC_UADDR16 25
|
||||
#define R_PPC_REL32 26
|
||||
#define R_PPC_PLT32 27
|
||||
#define R_PPC_PLTREL32 28
|
||||
#define R_PPC_PLT16_LO 29
|
||||
#define R_PPC_PLT16_HI 30
|
||||
#define R_PPC_PLT16_HA 31
|
||||
#define R_PPC_SDAREL16 32
|
||||
#define R_PPC_SECTOFF 33
|
||||
#define R_PPC_SECTOFF_LO 34
|
||||
#define R_PPC_SECTOFF_HI 35
|
||||
#define R_PPC_SECTOFF_HA 36
|
||||
|
||||
/*
|
||||
* TLS relocations
|
||||
*/
|
||||
#define R_PPC_TLS 67
|
||||
#define R_PPC_DTPMOD32 68
|
||||
#define R_PPC_TPREL16 69
|
||||
#define R_PPC_TPREL16_LO 70
|
||||
#define R_PPC_TPREL16_HI 71
|
||||
#define R_PPC_TPREL16_HA 72
|
||||
#define R_PPC_TPREL32 73
|
||||
#define R_PPC_DTPREL16 74
|
||||
#define R_PPC_DTPREL16_LO 75
|
||||
#define R_PPC_DTPREL16_HI 76
|
||||
#define R_PPC_DTPREL16_HA 77
|
||||
#define R_PPC_DTPREL32 78
|
||||
#define R_PPC_GOT_TLSGD16 79
|
||||
#define R_PPC_GOT_TLSGD16_LO 80
|
||||
#define R_PPC_GOT_TLSGD16_HI 81
|
||||
#define R_PPC_GOT_TLSGD16_HA 82
|
||||
#define R_PPC_GOT_TLSLD16 83
|
||||
#define R_PPC_GOT_TLSLD16_LO 84
|
||||
#define R_PPC_GOT_TLSLD16_HI 85
|
||||
#define R_PPC_GOT_TLSLD16_HA 86
|
||||
#define R_PPC_GOT_TPREL16 87
|
||||
#define R_PPC_GOT_TPREL16_LO 88
|
||||
#define R_PPC_GOT_TPREL16_HI 89
|
||||
#define R_PPC_GOT_TPREL16_HA 90
|
||||
|
||||
/*
|
||||
* The remaining relocs are from the Embedded ELF ABI, and are not in the
|
||||
* SVR4 ELF ABI.
|
||||
*/
|
||||
|
||||
#define R_PPC_EMB_NADDR32 101
|
||||
#define R_PPC_EMB_NADDR16 102
|
||||
#define R_PPC_EMB_NADDR16_LO 103
|
||||
#define R_PPC_EMB_NADDR16_HI 104
|
||||
#define R_PPC_EMB_NADDR16_HA 105
|
||||
#define R_PPC_EMB_SDAI16 106
|
||||
#define R_PPC_EMB_SDA2I16 107
|
||||
#define R_PPC_EMB_SDA2REL 108
|
||||
#define R_PPC_EMB_SDA21 109
|
||||
#define R_PPC_EMB_MRKREF 110
|
||||
#define R_PPC_EMB_RELSEC16 111
|
||||
#define R_PPC_EMB_RELST_LO 112
|
||||
#define R_PPC_EMB_RELST_HI 113
|
||||
#define R_PPC_EMB_RELST_HA 114
|
||||
#define R_PPC_EMB_BIT_FLD 115
|
||||
#define R_PPC_EMB_RELSDA 116
|
||||
|
||||
#define R_SPARC_NONE 0
|
||||
#define R_SPARC_8 1
|
||||
#define R_SPARC_16 2
|
||||
#define R_SPARC_32 3
|
||||
#define R_SPARC_DISP8 4
|
||||
#define R_SPARC_DISP16 5
|
||||
#define R_SPARC_DISP32 6
|
||||
#define R_SPARC_WDISP30 7
|
||||
#define R_SPARC_WDISP22 8
|
||||
#define R_SPARC_HI22 9
|
||||
#define R_SPARC_22 10
|
||||
#define R_SPARC_13 11
|
||||
#define R_SPARC_LO10 12
|
||||
#define R_SPARC_GOT10 13
|
||||
#define R_SPARC_GOT13 14
|
||||
#define R_SPARC_GOT22 15
|
||||
#define R_SPARC_PC10 16
|
||||
#define R_SPARC_PC22 17
|
||||
#define R_SPARC_WPLT30 18
|
||||
#define R_SPARC_COPY 19
|
||||
#define R_SPARC_GLOB_DAT 20
|
||||
#define R_SPARC_JMP_SLOT 21
|
||||
#define R_SPARC_RELATIVE 22
|
||||
#define R_SPARC_UA32 23
|
||||
#define R_SPARC_PLT32 24
|
||||
#define R_SPARC_HIPLT22 25
|
||||
#define R_SPARC_LOPLT10 26
|
||||
#define R_SPARC_PCPLT32 27
|
||||
#define R_SPARC_PCPLT22 28
|
||||
#define R_SPARC_PCPLT10 29
|
||||
#define R_SPARC_10 30
|
||||
#define R_SPARC_11 31
|
||||
#define R_SPARC_64 32
|
||||
#define R_SPARC_OLO10 33
|
||||
#define R_SPARC_HH22 34
|
||||
#define R_SPARC_HM10 35
|
||||
#define R_SPARC_LM22 36
|
||||
#define R_SPARC_PC_HH22 37
|
||||
#define R_SPARC_PC_HM10 38
|
||||
#define R_SPARC_PC_LM22 39
|
||||
#define R_SPARC_WDISP16 40
|
||||
#define R_SPARC_WDISP19 41
|
||||
#define R_SPARC_GLOB_JMP 42
|
||||
#define R_SPARC_7 43
|
||||
#define R_SPARC_5 44
|
||||
#define R_SPARC_6 45
|
||||
#define R_SPARC_DISP64 46
|
||||
#define R_SPARC_PLT64 47
|
||||
#define R_SPARC_HIX22 48
|
||||
#define R_SPARC_LOX10 49
|
||||
#define R_SPARC_H44 50
|
||||
#define R_SPARC_M44 51
|
||||
#define R_SPARC_L44 52
|
||||
#define R_SPARC_REGISTER 53
|
||||
#define R_SPARC_UA64 54
|
||||
#define R_SPARC_UA16 55
|
||||
#define R_SPARC_TLS_GD_HI22 56
|
||||
#define R_SPARC_TLS_GD_LO10 57
|
||||
#define R_SPARC_TLS_GD_ADD 58
|
||||
#define R_SPARC_TLS_GD_CALL 59
|
||||
#define R_SPARC_TLS_LDM_HI22 60
|
||||
#define R_SPARC_TLS_LDM_LO10 61
|
||||
#define R_SPARC_TLS_LDM_ADD 62
|
||||
#define R_SPARC_TLS_LDM_CALL 63
|
||||
#define R_SPARC_TLS_LDO_HIX22 64
|
||||
#define R_SPARC_TLS_LDO_LOX10 65
|
||||
#define R_SPARC_TLS_LDO_ADD 66
|
||||
#define R_SPARC_TLS_IE_HI22 67
|
||||
#define R_SPARC_TLS_IE_LO10 68
|
||||
#define R_SPARC_TLS_IE_LD 69
|
||||
#define R_SPARC_TLS_IE_LDX 70
|
||||
#define R_SPARC_TLS_IE_ADD 71
|
||||
#define R_SPARC_TLS_LE_HIX22 72
|
||||
#define R_SPARC_TLS_LE_LOX10 73
|
||||
#define R_SPARC_TLS_DTPMOD32 74
|
||||
#define R_SPARC_TLS_DTPMOD64 75
|
||||
#define R_SPARC_TLS_DTPOFF32 76
|
||||
#define R_SPARC_TLS_DTPOFF64 77
|
||||
#define R_SPARC_TLS_TPOFF32 78
|
||||
#define R_SPARC_TLS_TPOFF64 79
|
||||
|
||||
#define R_X86_64_NONE 0 /* No relocation. */
|
||||
#define R_X86_64_64 1 /* Add 64 bit symbol value. */
|
||||
#define R_X86_64_PC32 2 /* PC-relative 32 bit signed sym value. */
|
||||
#define R_X86_64_GOT32 3 /* PC-relative 32 bit GOT offset. */
|
||||
#define R_X86_64_PLT32 4 /* PC-relative 32 bit PLT offset. */
|
||||
#define R_X86_64_COPY 5 /* Copy data from shared object. */
|
||||
#define R_X86_64_GLOB_DAT 6 /* Set GOT entry to data address. */
|
||||
#define R_X86_64_JMP_SLOT 7 /* Set GOT entry to code address. */
|
||||
#define R_X86_64_RELATIVE 8 /* Add load address of shared object. */
|
||||
#define R_X86_64_GOTPCREL 9 /* Add 32 bit signed pcrel offset to GOT. */
|
||||
#define R_X86_64_32 10 /* Add 32 bit zero extended symbol value */
|
||||
#define R_X86_64_32S 11 /* Add 32 bit sign extended symbol value */
|
||||
#define R_X86_64_16 12 /* Add 16 bit zero extended symbol value */
|
||||
#define R_X86_64_PC16 13 /* Add 16 bit signed extended pc relative symbol value */
|
||||
#define R_X86_64_8 14 /* Add 8 bit zero extended symbol value */
|
||||
#define R_X86_64_PC8 15 /* Add 8 bit signed extended pc relative symbol value */
|
||||
#define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */
|
||||
#define R_X86_64_DTPOFF64 17 /* Offset in TLS block */
|
||||
#define R_X86_64_TPOFF64 18 /* Offset in static TLS block */
|
||||
#define R_X86_64_TLSGD 19 /* PC relative offset to GD GOT entry */
|
||||
#define R_X86_64_TLSLD 20 /* PC relative offset to LD GOT entry */
|
||||
#define R_X86_64_DTPOFF32 21 /* Offset in TLS block */
|
||||
#define R_X86_64_GOTTPOFF 22 /* PC relative offset to IE GOT entry */
|
||||
#define R_X86_64_TPOFF32 23 /* Offset in static TLS block */
|
||||
|
||||
|
||||
#endif /* !_SYS_ELF_COMMON_H_ */
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008,2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,148 +24,183 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_data.c 3258 2015-11-20 18:59:43Z emaste $");
|
||||
|
||||
Elf_Data *
|
||||
elf_getdata(Elf_Scn *s, Elf_Data *d)
|
||||
elf_getdata(Elf_Scn *s, Elf_Data *ed)
|
||||
{
|
||||
Elf *e;
|
||||
char *dst;
|
||||
size_t fsz, msz, count;
|
||||
int elfclass, elftype;
|
||||
unsigned int sh_type;
|
||||
uint64_t sh_align, sh_offset, sh_size;
|
||||
void (*xlate)(char *_d, char *_s, size_t _c, int _swap);
|
||||
Elf *e;
|
||||
unsigned int sh_type;
|
||||
int elfclass, elftype;
|
||||
size_t count, fsz, msz;
|
||||
struct _Libelf_Data *d;
|
||||
uint64_t sh_align, sh_offset, sh_size;
|
||||
int (*xlate)(unsigned char *_d, size_t _dsz, unsigned char *_s,
|
||||
size_t _c, int _swap);
|
||||
|
||||
if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
|
||||
(d != NULL && s != d->d_scn)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (s == NULL || (e = s->s_elf) == NULL ||
|
||||
(d != NULL && s != d->d_scn)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(e->e_kind == ELF_K_ELF);
|
||||
|
||||
if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
|
||||
return (&d->d_data);
|
||||
|
||||
if (d != NULL)
|
||||
return (&STAILQ_NEXT(d, d_next)->d_data);
|
||||
|
||||
if (e->e_rawfile == NULL) {
|
||||
/*
|
||||
* In the ELF_C_WRITE case, there is no source that
|
||||
* can provide data for the section.
|
||||
*/
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
elfclass = e->e_class;
|
||||
|
||||
assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
|
||||
|
||||
if (elfclass == ELFCLASS32) {
|
||||
sh_type = s->s_shdr.s_shdr32.sh_type;
|
||||
sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
|
||||
sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
|
||||
sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
|
||||
} else {
|
||||
sh_type = s->s_shdr.s_shdr64.sh_type;
|
||||
sh_offset = s->s_shdr.s_shdr64.sh_offset;
|
||||
sh_size = s->s_shdr.s_shdr64.sh_size;
|
||||
sh_align = s->s_shdr.s_shdr64.sh_addralign;
|
||||
}
|
||||
|
||||
if (sh_type == SHT_NULL) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
|
||||
elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
|
||||
sh_offset + sh_size > (uint64_t) e->e_rawsize)) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
|
||||
(elftype, (size_t) 1, e->e_version)) == 0) {
|
||||
LIBELF_SET_ERROR(UNIMPL, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (sh_size % fsz) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (sh_size / fsz > SIZE_MAX) {
|
||||
LIBELF_SET_ERROR(RANGE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
count = (size_t) (sh_size / fsz);
|
||||
|
||||
msz = _libelf_msize(elftype, elfclass, e->e_version);
|
||||
|
||||
if (count > 0 && msz > SIZE_MAX / count) {
|
||||
LIBELF_SET_ERROR(RANGE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(msz > 0);
|
||||
assert(count <= SIZE_MAX);
|
||||
assert(msz * count <= SIZE_MAX);
|
||||
|
||||
if ((d = _libelf_allocate_data(s)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
d->d_data.d_buf = NULL;
|
||||
d->d_data.d_off = 0;
|
||||
d->d_data.d_align = sh_align;
|
||||
d->d_data.d_size = msz * count;
|
||||
d->d_data.d_type = elftype;
|
||||
d->d_data.d_version = e->e_version;
|
||||
|
||||
if (sh_type == SHT_NOBITS || sh_size == 0) {
|
||||
STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
|
||||
return (&d->d_data);
|
||||
}
|
||||
|
||||
if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
|
||||
return (d);
|
||||
if ((d->d_data.d_buf = malloc(msz * count)) == NULL) {
|
||||
(void) _libelf_release_data(d);
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (d != NULL)
|
||||
return (STAILQ_NEXT(d, d_next));
|
||||
d->d_flags |= LIBELF_F_DATA_MALLOCED;
|
||||
|
||||
if (e->e_rawfile == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
|
||||
if (!(*xlate)(d->d_data.d_buf, (size_t) d->d_data.d_size,
|
||||
e->e_rawfile + sh_offset, count,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder))) {
|
||||
_libelf_release_data(d);
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
elfclass = e->e_class;
|
||||
STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
|
||||
|
||||
assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
|
||||
|
||||
if (elfclass == ELFCLASS32) {
|
||||
sh_type = s->s_shdr.s_shdr32.sh_type;
|
||||
sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
|
||||
sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
|
||||
sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
|
||||
} else {
|
||||
sh_type = s->s_shdr.s_shdr64.sh_type;
|
||||
sh_offset = s->s_shdr.s_shdr64.sh_offset;
|
||||
sh_size = s->s_shdr.s_shdr64.sh_size;
|
||||
sh_align = s->s_shdr.s_shdr64.sh_addralign;
|
||||
}
|
||||
|
||||
if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
|
||||
elftype > ELF_T_LAST ||
|
||||
sh_offset + sh_size > (uint64_t) e->e_rawsize) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)(elftype,
|
||||
(size_t) 1, e->e_version)) == 0) {
|
||||
LIBELF_SET_ERROR(UNIMPL, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
if (sh_size % fsz) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
count = sh_size / fsz;
|
||||
|
||||
msz = _libelf_msize(elftype, elfclass, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
|
||||
if ((dst = malloc(msz*count)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((d = _libelf_allocate_data(s)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
d->d_buf = dst;
|
||||
d->d_off = 0;
|
||||
d->d_align = sh_align;
|
||||
d->d_size = msz * count;
|
||||
d->d_type = elftype;
|
||||
d->d_version = e->e_version;
|
||||
|
||||
d->d_flags |= LIBELF_F_MALLOCED;
|
||||
STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
|
||||
|
||||
xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
|
||||
(*xlate)(d->d_buf, e->e_rawfile + sh_offset, count, e->e_byteorder !=
|
||||
LIBELF_PRIVATE(byteorder));
|
||||
|
||||
return (d);
|
||||
return (&d->d_data);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
elf_newdata(Elf_Scn *s)
|
||||
{
|
||||
Elf *e;
|
||||
Elf_Data *d;
|
||||
Elf *e;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (s == NULL || (e = s->s_elf) == NULL ||
|
||||
e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (s == NULL || (e = s->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* elf_newdata() has to append a data descriptor, so
|
||||
* bring in existing section data if not already present.
|
||||
*/
|
||||
if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
|
||||
if (elf_getdata(s, NULL) == NULL)
|
||||
return (NULL);
|
||||
assert(e->e_kind == ELF_K_ELF);
|
||||
|
||||
if ((d = malloc(sizeof(Elf_Data))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, errno);
|
||||
return (NULL);
|
||||
}
|
||||
/*
|
||||
* elf_newdata() has to append a data descriptor, so
|
||||
* bring in existing section data if not already present.
|
||||
*/
|
||||
if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
|
||||
if (elf_getdata(s, NULL) == NULL)
|
||||
return (NULL);
|
||||
|
||||
STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
|
||||
d->d_flags = 0;
|
||||
d->d_scn = s;
|
||||
if ((d = _libelf_allocate_data(s)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
d->d_align = 1;
|
||||
d->d_buf = NULL;
|
||||
d->d_off = (uint64_t) ~0;
|
||||
d->d_size = 0;
|
||||
d->d_type = ELF_T_BYTE;
|
||||
d->d_version = LIBELF_PRIVATE(version);
|
||||
STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
|
||||
|
||||
(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
|
||||
d->d_data.d_align = 1;
|
||||
d->d_data.d_buf = NULL;
|
||||
d->d_data.d_off = (uint64_t) ~0;
|
||||
d->d_data.d_size = 0;
|
||||
d->d_data.d_type = ELF_T_BYTE;
|
||||
d->d_data.d_version = LIBELF_PRIVATE(version);
|
||||
|
||||
return (d);
|
||||
(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
return (&d->d_data);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -174,49 +209,68 @@ elf_newdata(Elf_Scn *s)
|
||||
*/
|
||||
|
||||
Elf_Data *
|
||||
elf_rawdata(Elf_Scn *s, Elf_Data *d)
|
||||
elf_rawdata(Elf_Scn *s, Elf_Data *ed)
|
||||
{
|
||||
Elf *e;
|
||||
int elf_class;
|
||||
uint64_t sh_align, sh_offset, sh_size;
|
||||
Elf *e;
|
||||
int elf_class;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
uint64_t sh_align, sh_offset, sh_size;
|
||||
|
||||
if (s == NULL || (e = s->s_elf) == NULL ||
|
||||
e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (s == NULL || (e = s->s_elf) == NULL || e->e_rawfile == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
|
||||
return (d);
|
||||
assert(e->e_kind == ELF_K_ELF);
|
||||
|
||||
if (d != NULL)
|
||||
return (STAILQ_NEXT(d, d_next));
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
elf_class = e->e_class;
|
||||
if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
|
||||
return (&d->d_data);
|
||||
|
||||
assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
|
||||
if (d != NULL)
|
||||
return (&STAILQ_NEXT(d, d_next)->d_data);
|
||||
|
||||
if (elf_class == ELFCLASS32) {
|
||||
sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
|
||||
sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
|
||||
sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
|
||||
} else {
|
||||
sh_offset = s->s_shdr.s_shdr64.sh_offset;
|
||||
sh_size = s->s_shdr.s_shdr64.sh_size;
|
||||
sh_align = s->s_shdr.s_shdr64.sh_addralign;
|
||||
}
|
||||
elf_class = e->e_class;
|
||||
|
||||
if ((d = _libelf_allocate_data(s)) == NULL)
|
||||
return (NULL);
|
||||
assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
|
||||
|
||||
d->d_buf = e->e_rawfile + sh_offset;
|
||||
d->d_off = 0;
|
||||
d->d_align = sh_align;
|
||||
d->d_size = sh_size;
|
||||
d->d_type = ELF_T_BYTE;
|
||||
d->d_version = e->e_version;
|
||||
if (elf_class == ELFCLASS32) {
|
||||
sh_type = s->s_shdr.s_shdr32.sh_type;
|
||||
sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
|
||||
sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
|
||||
sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
|
||||
} else {
|
||||
sh_type = s->s_shdr.s_shdr64.sh_type;
|
||||
sh_offset = s->s_shdr.s_shdr64.sh_offset;
|
||||
sh_size = s->s_shdr.s_shdr64.sh_size;
|
||||
sh_align = s->s_shdr.s_shdr64.sh_addralign;
|
||||
}
|
||||
|
||||
STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
|
||||
if (sh_type == SHT_NULL) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (d);
|
||||
if (sh_type != SHT_NOBITS &&
|
||||
sh_offset + sh_size > (uint64_t) e->e_rawsize) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((d = _libelf_allocate_data(s)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
d->d_data.d_buf = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
|
||||
e->e_rawfile + sh_offset;
|
||||
d->d_data.d_off = 0;
|
||||
d->d_data.d_align = sh_align;
|
||||
d->d_data.d_size = sh_size;
|
||||
d->d_data.d_type = ELF_T_BYTE;
|
||||
d->d_data.d_version = e->e_version;
|
||||
|
||||
STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
|
||||
|
||||
return (&d->d_data);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2009,2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,63 +24,72 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
#if ELFTC_HAVE_MMAP
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
ELFTC_VCSID("$Id: elf_end.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
int
|
||||
elf_end(Elf *e)
|
||||
{
|
||||
Elf *sv;
|
||||
Elf_Scn *scn, *tscn;
|
||||
Elf *sv;
|
||||
Elf_Scn *scn, *tscn;
|
||||
|
||||
if (e == NULL || e->e_activations == 0)
|
||||
return (0);
|
||||
if (e == NULL || e->e_activations == 0)
|
||||
return (0);
|
||||
|
||||
if (--e->e_activations > 0)
|
||||
return (e->e_activations);
|
||||
if (--e->e_activations > 0)
|
||||
return (e->e_activations);
|
||||
|
||||
assert(e->e_activations == 0);
|
||||
assert(e->e_activations == 0);
|
||||
|
||||
while (e && e->e_activations == 0) {
|
||||
switch (e->e_kind) {
|
||||
case ELF_K_AR:
|
||||
/*
|
||||
* If we still have open child descriptors, we
|
||||
* need to defer reclaiming resources till all
|
||||
* the child descriptors for the archive are
|
||||
* closed.
|
||||
*/
|
||||
if (e->e_u.e_ar.e_nchildren > 0)
|
||||
return (0);
|
||||
break;
|
||||
case ELF_K_ELF:
|
||||
/*
|
||||
* Reclaim all section descriptors.
|
||||
*/
|
||||
STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn)
|
||||
scn = _libelf_release_scn(scn);
|
||||
break;
|
||||
case ELF_K_NUM:
|
||||
assert(0);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
while (e && e->e_activations == 0) {
|
||||
switch (e->e_kind) {
|
||||
case ELF_K_AR:
|
||||
/*
|
||||
* If we still have open child descriptors, we
|
||||
* need to defer reclaiming resources till all
|
||||
* the child descriptors for the archive are
|
||||
* closed.
|
||||
*/
|
||||
if (e->e_u.e_ar.e_nchildren > 0)
|
||||
return (0);
|
||||
break;
|
||||
case ELF_K_ELF:
|
||||
/*
|
||||
* Reclaim all section descriptors.
|
||||
*/
|
||||
STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next,
|
||||
tscn)
|
||||
scn = _libelf_release_scn(scn);
|
||||
break;
|
||||
case ELF_K_NUM:
|
||||
assert(0);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (e->e_flags & LIBELF_F_MMAP)
|
||||
(void) munmap(e->e_rawfile, e->e_rawsize);
|
||||
if (e->e_rawfile) {
|
||||
if (e->e_flags & LIBELF_F_RAWFILE_MALLOC)
|
||||
free(e->e_rawfile);
|
||||
#if ELFTC_HAVE_MMAP
|
||||
else if (e->e_flags & LIBELF_F_RAWFILE_MMAP)
|
||||
(void) munmap(e->e_rawfile, e->e_rawsize);
|
||||
#endif
|
||||
}
|
||||
|
||||
sv = e;
|
||||
if ((e = e->e_parent) != NULL)
|
||||
e->e_u.e_ar.e_nchildren--;
|
||||
sv = _libelf_release_elf(sv);
|
||||
}
|
||||
sv = e;
|
||||
if ((e = e->e_parent) != NULL)
|
||||
e->e_u.e_ar.e_nchildren--;
|
||||
sv = _libelf_release_elf(sv);
|
||||
}
|
||||
|
||||
return (0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008,2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,59 +24,60 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <libelf.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
#include "libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_errmsg.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/*
|
||||
* Retrieve a human readable translation for an error message.
|
||||
*/
|
||||
|
||||
const char *_libelf_errors[] = {
|
||||
static const char *_libelf_errors[] = {
|
||||
#define DEFINE_ERROR(N,S) [ELF_E_##N] = S
|
||||
DEFINE_ERROR(NONE, "No Error"),
|
||||
DEFINE_ERROR(ARCHIVE, "Malformed ar(1) archive"),
|
||||
DEFINE_ERROR(ARGUMENT, "Invalid argument"),
|
||||
DEFINE_ERROR(CLASS, "ELF class mismatch"),
|
||||
DEFINE_ERROR(DATA, "Invalid data buffer descriptor"),
|
||||
DEFINE_ERROR(HEADER, "Missing or malformed ELF header"),
|
||||
DEFINE_ERROR(IO, "I/O error"),
|
||||
DEFINE_ERROR(LAYOUT, "Layout constraint violation"),
|
||||
DEFINE_ERROR(MODE, "Incorrect ELF descriptor mode"),
|
||||
DEFINE_ERROR(RANGE, "Value out of range of target"),
|
||||
DEFINE_ERROR(RESOURCE, "Resource exhaustion"),
|
||||
DEFINE_ERROR(SECTION, "Invalid section descriptor"),
|
||||
DEFINE_ERROR(SEQUENCE, "API calls out of sequence"),
|
||||
DEFINE_ERROR(UNIMPL, "Unimplemented feature"),
|
||||
DEFINE_ERROR(VERSION, "Unknown ELF API version"),
|
||||
DEFINE_ERROR(NUM, "Unknown error")
|
||||
DEFINE_ERROR(NONE, "No Error"),
|
||||
DEFINE_ERROR(ARCHIVE, "Malformed ar(1) archive"),
|
||||
DEFINE_ERROR(ARGUMENT, "Invalid argument"),
|
||||
DEFINE_ERROR(CLASS, "ELF class mismatch"),
|
||||
DEFINE_ERROR(DATA, "Invalid data buffer descriptor"),
|
||||
DEFINE_ERROR(HEADER, "Missing or malformed ELF header"),
|
||||
DEFINE_ERROR(IO, "I/O error"),
|
||||
DEFINE_ERROR(LAYOUT, "Layout constraint violation"),
|
||||
DEFINE_ERROR(MODE, "Incorrect ELF descriptor mode"),
|
||||
DEFINE_ERROR(RANGE, "Value out of range of target"),
|
||||
DEFINE_ERROR(RESOURCE, "Resource exhaustion"),
|
||||
DEFINE_ERROR(SECTION, "Invalid section descriptor"),
|
||||
DEFINE_ERROR(SEQUENCE, "API calls out of sequence"),
|
||||
DEFINE_ERROR(UNIMPL, "Unimplemented feature"),
|
||||
DEFINE_ERROR(VERSION, "Unknown ELF API version"),
|
||||
DEFINE_ERROR(NUM, "Unknown error")
|
||||
#undef DEFINE_ERROR
|
||||
};
|
||||
|
||||
const char *
|
||||
elf_errmsg(int error)
|
||||
{
|
||||
int oserr;
|
||||
int oserr;
|
||||
|
||||
if (error == 0 && (error = LIBELF_PRIVATE(error)) == 0)
|
||||
return NULL;
|
||||
else if (error == -1)
|
||||
error = LIBELF_PRIVATE(error);
|
||||
if (error == ELF_E_NONE &&
|
||||
(error = LIBELF_PRIVATE(error)) == 0)
|
||||
return NULL;
|
||||
else if (error == -1)
|
||||
error = LIBELF_PRIVATE(error);
|
||||
|
||||
oserr = error >> LIBELF_OS_ERROR_SHIFT;
|
||||
error &= LIBELF_ELF_ERROR_MASK;
|
||||
oserr = error >> LIBELF_OS_ERROR_SHIFT;
|
||||
error &= LIBELF_ELF_ERROR_MASK;
|
||||
|
||||
if (error < 0 || error >= ELF_E_NUM)
|
||||
return _libelf_errors[ELF_E_NUM];
|
||||
if (oserr) {
|
||||
strncpy(LIBELF_PRIVATE(msg), _libelf_errors[error],
|
||||
sizeof(LIBELF_PRIVATE(msg)));
|
||||
strncat(LIBELF_PRIVATE(msg), ": ", sizeof(LIBELF_PRIVATE(msg)));
|
||||
strncat(LIBELF_PRIVATE(msg), strerror(oserr),
|
||||
sizeof(LIBELF_PRIVATE(msg)));
|
||||
return (const char *)&LIBELF_PRIVATE(msg);
|
||||
}
|
||||
return _libelf_errors[error];
|
||||
if (error < ELF_E_NONE || error >= ELF_E_NUM)
|
||||
return _libelf_errors[ELF_E_NUM];
|
||||
if (oserr) {
|
||||
(void) snprintf((char *) LIBELF_PRIVATE(msg),
|
||||
sizeof(LIBELF_PRIVATE(msg)), "%s: %s",
|
||||
_libelf_errors[error], strerror(oserr));
|
||||
return (const char *)&LIBELF_PRIVATE(msg);
|
||||
}
|
||||
return _libelf_errors[error];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008,2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,17 +24,18 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_errno.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
int
|
||||
elf_errno(void)
|
||||
{
|
||||
int old;
|
||||
int old;
|
||||
|
||||
old = LIBELF_PRIVATE(error);
|
||||
LIBELF_PRIVATE(error) = 0;
|
||||
return (old & LIBELF_ELF_ERROR_MASK);
|
||||
old = LIBELF_PRIVATE(error);
|
||||
LIBELF_PRIVATE(error) = 0;
|
||||
return (old & LIBELF_ELF_ERROR_MASK);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,13 +24,14 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_fill.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
void
|
||||
elf_fill(int fill)
|
||||
{
|
||||
LIBELF_PRIVATE(fillchar) = fill;
|
||||
LIBELF_PRIVATE(fillchar) = fill;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2009,2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,139 +24,173 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_flag.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
unsigned int
|
||||
elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
unsigned int r;
|
||||
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(flags & ~ELF_F_DIRTY) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (c == ELF_C_SET)
|
||||
r = a->ar_flags |= flags;
|
||||
else
|
||||
r = a->ar_flags &= ~flags;
|
||||
|
||||
return (r & LIBELF_F_API_MASK);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
unsigned int r;
|
||||
unsigned int r;
|
||||
struct _Libelf_Data *ld;
|
||||
|
||||
if (d == NULL)
|
||||
return (0);
|
||||
if (d == NULL)
|
||||
return (0);
|
||||
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) || (scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
|
||||
(flags & ~ELF_F_DIRTY) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(flags & ~ELF_F_DIRTY) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (c == ELF_C_SET)
|
||||
r = scn->s_flags |= flags;
|
||||
else
|
||||
r = scn->s_flags &= ~flags;
|
||||
ld = (struct _Libelf_Data *) d;
|
||||
|
||||
return (r);
|
||||
if (c == ELF_C_SET)
|
||||
r = ld->d_flags |= flags;
|
||||
else
|
||||
r = ld->d_flags &= ~flags;
|
||||
|
||||
return (r & LIBELF_F_API_MASK);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
int ec;
|
||||
void *ehdr;
|
||||
int ec;
|
||||
void *ehdr;
|
||||
|
||||
if (e == NULL)
|
||||
return (0);
|
||||
if (e == NULL)
|
||||
return (0);
|
||||
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32;
|
||||
else
|
||||
ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64;
|
||||
if (ec == ELFCLASS32)
|
||||
ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32;
|
||||
else
|
||||
ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64;
|
||||
|
||||
if (ehdr == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ehdr == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
return (elf_flagelf(e, c, flags));
|
||||
return (elf_flagelf(e, c, flags));
|
||||
}
|
||||
|
||||
unsigned int
|
||||
elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
int r;
|
||||
unsigned int r;
|
||||
|
||||
if (e == NULL)
|
||||
return (0);
|
||||
if (e == NULL)
|
||||
return (0);
|
||||
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(e->e_kind != ELF_K_ELF) ||
|
||||
(flags & ~(ELF_F_DIRTY|ELF_F_LAYOUT)) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(e->e_kind != ELF_K_ELF) ||
|
||||
(flags & ~(ELF_F_ARCHIVE | ELF_F_ARCHIVE_SYSV |
|
||||
ELF_F_DIRTY | ELF_F_LAYOUT)) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (c == ELF_C_SET)
|
||||
r = e->e_flags |= flags;
|
||||
else
|
||||
r = e->e_flags &= ~flags;
|
||||
return (r);
|
||||
if ((flags & ELF_F_ARCHIVE_SYSV) && (flags & ELF_F_ARCHIVE) == 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((flags & ELF_F_ARCHIVE) && e->e_cmd != ELF_C_WRITE) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (c == ELF_C_SET)
|
||||
r = e->e_flags |= flags;
|
||||
else
|
||||
r = e->e_flags &= ~flags;
|
||||
return (r & LIBELF_F_API_MASK);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
int ec;
|
||||
void *phdr;
|
||||
int ec;
|
||||
void *phdr;
|
||||
|
||||
if (e == NULL)
|
||||
return (0);
|
||||
if (e == NULL)
|
||||
return (0);
|
||||
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
phdr = e->e_u.e_elf.e_phdr.e_phdr32;
|
||||
else
|
||||
phdr = e->e_u.e_elf.e_phdr.e_phdr64;
|
||||
if (ec == ELFCLASS32)
|
||||
phdr = e->e_u.e_elf.e_phdr.e_phdr32;
|
||||
else
|
||||
phdr = e->e_u.e_elf.e_phdr.e_phdr64;
|
||||
|
||||
if (phdr == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (0);
|
||||
}
|
||||
if (phdr == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
return (elf_flagelf(e, c, flags));
|
||||
return (elf_flagelf(e, c, flags));
|
||||
}
|
||||
|
||||
unsigned int
|
||||
elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
int r;
|
||||
unsigned int r;
|
||||
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
if (s == NULL)
|
||||
return (0);
|
||||
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(flags & ~ELF_F_DIRTY) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if ((c != ELF_C_SET && c != ELF_C_CLR) ||
|
||||
(flags & ~ELF_F_DIRTY) != 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (c == ELF_C_SET)
|
||||
r = s->s_flags |= flags;
|
||||
else
|
||||
r = s->s_flags &= ~flags;
|
||||
return (r);
|
||||
if (c == ELF_C_SET)
|
||||
r = s->s_flags |= flags;
|
||||
else
|
||||
r = s->s_flags &= ~flags;
|
||||
return (r & LIBELF_F_API_MASK);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
|
||||
{
|
||||
return (elf_flagscn(s, c, flags));
|
||||
return (elf_flagscn(s, c, flags));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008,2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,23 +24,22 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_getarhdr.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
Elf_Arhdr *
|
||||
elf_getarhdr(Elf *e)
|
||||
{
|
||||
Elf_Arhdr *arh;
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e->e_flags & LIBELF_F_AR_HEADER)
|
||||
return (e->e_hdr.e_arhdr);
|
||||
|
||||
if ((arh = e->e_arhdr) != NULL)
|
||||
return (arh);
|
||||
|
||||
return (_libelf_ar_gethdr(e));
|
||||
return (_libelf_ar_gethdr(e));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,31 +24,33 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_getarsym.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
Elf_Arsym *
|
||||
elf_getarsym(Elf *ar, size_t *ptr)
|
||||
{
|
||||
size_t n;
|
||||
Elf_Arsym *symtab;
|
||||
size_t n;
|
||||
Elf_Arsym *symtab;
|
||||
|
||||
n = 0;
|
||||
symtab = NULL;
|
||||
n = 0;
|
||||
symtab = NULL;
|
||||
|
||||
if (ar == NULL || ar->e_kind != ELF_K_AR)
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
else if ((symtab = ar->e_u.e_ar.e_symtab) != NULL)
|
||||
n = ar->e_u.e_ar.e_symtabsz;
|
||||
else if (ar->e_u.e_ar.e_rawsymtab)
|
||||
symtab = _libelf_ar_process_symtab(ar, &n);
|
||||
else
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
if (ar == NULL || ar->e_kind != ELF_K_AR)
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
else if ((symtab = ar->e_u.e_ar.e_symtab) != NULL)
|
||||
n = ar->e_u.e_ar.e_symtabsz;
|
||||
else if (ar->e_u.e_ar.e_rawsymtab)
|
||||
symtab = (ar->e_flags & LIBELF_F_AR_VARIANT_SVR4) ?
|
||||
_libelf_ar_process_svr4_symtab(ar, &n) :
|
||||
_libelf_ar_process_bsd_symtab(ar, &n);
|
||||
else
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
|
||||
if (ptr)
|
||||
*ptr = n;
|
||||
return (symtab);
|
||||
if (ptr)
|
||||
*ptr = n;
|
||||
return (symtab);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,20 +24,23 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_getbase.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
off_t
|
||||
elf_getbase(Elf *e)
|
||||
{
|
||||
if (e == NULL ||
|
||||
e->e_parent == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (off_t) -1;
|
||||
}
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return ((off_t) -1);
|
||||
}
|
||||
|
||||
return ((off_t) ((uintptr_t) e->e_rawfile -
|
||||
(uintptr_t) e->e_parent->e_rawfile));
|
||||
if (e->e_parent == NULL)
|
||||
return ((off_t) 0);
|
||||
|
||||
return ((off_t) ((uintptr_t) e->e_rawfile -
|
||||
(uintptr_t) e->e_parent->e_rawfile));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,42 +24,43 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include <assert.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_getident.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
char *
|
||||
elf_getident(Elf *e, size_t *sz)
|
||||
{
|
||||
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
goto error;
|
||||
}
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (e->e_cmd == ELF_C_WRITE && e->e_rawfile == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
goto error;
|
||||
}
|
||||
if (e->e_cmd == ELF_C_WRITE && e->e_rawfile == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
goto error;
|
||||
}
|
||||
|
||||
assert(e->e_kind != ELF_K_AR || e->e_cmd == ELF_C_READ);
|
||||
assert(e->e_kind != ELF_K_AR || e->e_cmd == ELF_C_READ);
|
||||
|
||||
if (sz) {
|
||||
if (e->e_kind == ELF_K_AR)
|
||||
*sz = SARMAG;
|
||||
else if (e->e_kind == ELF_K_ELF)
|
||||
*sz = EI_NIDENT;
|
||||
else
|
||||
*sz = e->e_rawsize;
|
||||
}
|
||||
if (sz) {
|
||||
if (e->e_kind == ELF_K_AR)
|
||||
*sz = SARMAG;
|
||||
else if (e->e_kind == ELF_K_ELF)
|
||||
*sz = EI_NIDENT;
|
||||
else
|
||||
*sz = e->e_rawsize;
|
||||
}
|
||||
|
||||
return (e->e_rawfile);
|
||||
return ((char *) e->e_rawfile);
|
||||
|
||||
error:
|
||||
if (sz)
|
||||
*sz = 0;
|
||||
return (NULL);
|
||||
if (sz)
|
||||
*sz = 0;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,29 +24,31 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <libelf.h>
|
||||
|
||||
#include "libelf.h"
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_hash.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/*
|
||||
* This elf_hash function is defined by the System V ABI. It must be
|
||||
* kept compatible with "src/libexec/rtld-elf/rtld.c".
|
||||
* This elf_hash function is defined by the System V ABI.
|
||||
*/
|
||||
|
||||
unsigned long
|
||||
elf_hash(const char *name)
|
||||
{
|
||||
unsigned long h, t;
|
||||
const unsigned char *s;
|
||||
unsigned long h, t;
|
||||
const unsigned char *s;
|
||||
|
||||
s = (const unsigned char *) name;
|
||||
h = t = 0;
|
||||
s = (const unsigned char *) name;
|
||||
h = t = 0;
|
||||
|
||||
for (; *s != '\0'; h = h & ~t) {
|
||||
h = (h << 4) + *s++;
|
||||
t = h & 0xF0000000UL;
|
||||
if (t)
|
||||
h ^= t >> 24;
|
||||
}
|
||||
for (; *s != '\0'; h = h & ~t) {
|
||||
h = (h << 4) + *s++;
|
||||
t = h & 0xF0000000UL;
|
||||
if (t)
|
||||
h ^= t >> 24;
|
||||
}
|
||||
|
||||
return (h);
|
||||
return (h);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,18 +24,19 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_kind.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
Elf_Kind
|
||||
elf_kind(Elf *e)
|
||||
{
|
||||
if (e == NULL)
|
||||
return (ELF_K_NONE);
|
||||
if (e->e_kind == ELF_K_AR ||
|
||||
e->e_kind == ELF_K_ELF)
|
||||
return (e->e_kind);
|
||||
return (ELF_K_NONE);
|
||||
if (e == NULL)
|
||||
return (ELF_K_NONE);
|
||||
if (e->e_kind == ELF_K_AR ||
|
||||
e->e_kind == ELF_K_ELF)
|
||||
return (e->e_kind);
|
||||
return (ELF_K_NONE);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,66 +24,24 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include "libelf.h"
|
||||
#include <string.h>
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_memory.c 3013 2014-03-23 06:16:59Z jkoshy $");
|
||||
|
||||
Elf *
|
||||
elf_memory(char *image, size_t sz)
|
||||
{
|
||||
Elf *e;
|
||||
if (LIBELF_PRIVATE(version) == EV_NONE) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (LIBELF_PRIVATE(version) == EV_NONE) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (image == NULL || sz == 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (image == NULL || sz == 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((e = _libelf_allocate_elf()) == NULL)
|
||||
return (NULL);
|
||||
|
||||
e->e_cmd = ELF_C_READ;
|
||||
e->e_rawfile = image;
|
||||
e->e_rawsize = sz;
|
||||
|
||||
#undef LIBELF_IS_ELF
|
||||
#define LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && \
|
||||
(P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 && \
|
||||
(P)[EI_MAG3] == ELFMAG3)
|
||||
|
||||
if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) {
|
||||
_libelf_init_elf(e, ELF_K_ELF);
|
||||
e->e_class = image[EI_CLASS];
|
||||
e->e_byteorder = image[EI_DATA];
|
||||
e->e_version = image[EI_VERSION];
|
||||
|
||||
if (e->e_version > EV_CURRENT) {
|
||||
e = _libelf_release_elf(e);
|
||||
LIBELF_SET_ERROR(VERSION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((e->e_byteorder != ELFDATA2LSB && e->e_byteorder !=
|
||||
ELFDATA2MSB) || (e->e_class != ELFCLASS32 && e->e_class !=
|
||||
ELFCLASS64)) {
|
||||
e = _libelf_release_elf(e);
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
} else if (sz >= SARMAG &&
|
||||
strncmp(image, ARMAG, (size_t) SARMAG) == 0) {
|
||||
_libelf_init_elf(e, ELF_K_AR);
|
||||
e = _libelf_ar_open(e);
|
||||
} else
|
||||
_libelf_init_elf(e, ELF_K_NONE);
|
||||
|
||||
return (e);
|
||||
return (_libelf_memory((unsigned char *) image, sz, 1));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,36 +24,41 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include <assert.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_next.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
Elf_Cmd
|
||||
elf_next(Elf *e)
|
||||
{
|
||||
off_t next;
|
||||
Elf *parent;
|
||||
off_t next;
|
||||
Elf *parent;
|
||||
|
||||
if (e == NULL)
|
||||
return (ELF_C_NULL);
|
||||
if (e == NULL)
|
||||
return (ELF_C_NULL);
|
||||
|
||||
if ((parent = e->e_parent) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (ELF_C_NULL);
|
||||
}
|
||||
if ((parent = e->e_parent) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (ELF_C_NULL);
|
||||
}
|
||||
|
||||
assert (parent->e_kind == ELF_K_AR);
|
||||
assert (parent->e_cmd == ELF_C_READ);
|
||||
assert((uintptr_t) e->e_rawfile % 2 == 0);
|
||||
assert(e->e_rawfile > parent->e_rawfile);
|
||||
assert(parent->e_kind == ELF_K_AR);
|
||||
assert(parent->e_cmd == ELF_C_READ);
|
||||
assert(e->e_rawfile > parent->e_rawfile);
|
||||
|
||||
next = e->e_rawfile - parent->e_rawfile + e->e_rawsize;
|
||||
next = (next + 1) & ~1; /* round up to an even boundary */
|
||||
next = e->e_rawfile - parent->e_rawfile + (off_t) e->e_rawsize;
|
||||
next = (next + 1) & ~1; /* round up to an even boundary */
|
||||
|
||||
parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ? (off_t) 0 : next;
|
||||
/*
|
||||
* Setup the 'e_next' field of the archive descriptor for the
|
||||
* next call to 'elf_begin()'.
|
||||
*/
|
||||
parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ?
|
||||
(off_t) 0 : next;
|
||||
|
||||
return (ELF_C_READ);
|
||||
return (ELF_C_READ);
|
||||
}
|
||||
|
||||
67
ext/libelf/elf_open.c
Normal file
67
ext/libelf/elf_open.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/*-
|
||||
* Copyright (c) 2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id$");
|
||||
|
||||
/*
|
||||
* Extension API: open a file for reading, ignoring parse errors.
|
||||
*/
|
||||
|
||||
Elf *
|
||||
elf_open(int fd)
|
||||
{
|
||||
if (LIBELF_PRIVATE(version) == EV_NONE) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (_libelf_open_object(fd, ELF_C_READ, 0));
|
||||
}
|
||||
|
||||
/*
|
||||
* Extension API: create an ELF descriptor for an in-memory object,
|
||||
* ignoring parse errors.
|
||||
*/
|
||||
|
||||
Elf *
|
||||
elf_openmemory(char *image, size_t sz)
|
||||
{
|
||||
if (LIBELF_PRIVATE(version) == EV_NONE) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (image == NULL || sz == 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (_libelf_memory((unsigned char *) image, sz, 0));
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,28 +24,42 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_phnum.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
static int
|
||||
_libelf_getphdrnum(Elf *e, size_t *phnum)
|
||||
{
|
||||
void *eh;
|
||||
int ec;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (-1);
|
||||
|
||||
*phnum = e->e_u.e_elf.e_nphdr;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
elf_getphdrnum(Elf *e, size_t *phnum)
|
||||
{
|
||||
return (_libelf_getphdrnum(e, phnum));
|
||||
}
|
||||
|
||||
/* Deprecated API */
|
||||
int
|
||||
elf_getphnum(Elf *e, size_t *phnum)
|
||||
{
|
||||
void *eh;
|
||||
int ec;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
|
||||
*phnum = e->e_u.e_elf.e_nphdr;
|
||||
|
||||
return (1);
|
||||
return (_libelf_getphdrnum(e, phnum) >= 0);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,33 +24,34 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_rand.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
off_t
|
||||
elf_rand(Elf *ar, off_t offset)
|
||||
{
|
||||
struct ar_hdr *arh;
|
||||
struct ar_hdr *arh;
|
||||
|
||||
if (ar == NULL || ar->e_kind != ELF_K_AR ||
|
||||
(offset & 1) || offset < SARMAG ||
|
||||
offset + sizeof(struct ar_hdr) >= ar->e_rawsize) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return 0;
|
||||
}
|
||||
if (ar == NULL || ar->e_kind != ELF_K_AR ||
|
||||
(offset & 1) || offset < SARMAG ||
|
||||
(size_t) offset + sizeof(struct ar_hdr) >= ar->e_rawsize) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
arh = (struct ar_hdr *) (ar->e_rawfile + offset);
|
||||
arh = (struct ar_hdr *) (ar->e_rawfile + offset);
|
||||
|
||||
/* a too simple sanity check */
|
||||
if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return 0;
|
||||
}
|
||||
/* a too simple sanity check */
|
||||
if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ar->e_u.e_ar.e_next = offset;
|
||||
ar->e_u.e_ar.e_next = offset;
|
||||
|
||||
return (offset);
|
||||
return (offset);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,27 +24,28 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_rawfile.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
char *
|
||||
elf_rawfile(Elf *e, size_t *sz)
|
||||
{
|
||||
char *ptr;
|
||||
size_t size;
|
||||
size_t size;
|
||||
unsigned char *ptr;
|
||||
|
||||
size = e ? e->e_rawsize : 0;
|
||||
ptr = NULL;
|
||||
size = e ? e->e_rawsize : 0;
|
||||
ptr = NULL;
|
||||
|
||||
if (e == NULL)
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
else if ((ptr = e->e_rawfile) == NULL && e->e_cmd == ELF_C_WRITE)
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
if (e == NULL)
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
else if ((ptr = e->e_rawfile) == NULL && e->e_cmd == ELF_C_WRITE)
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
|
||||
if (sz)
|
||||
*sz = size;
|
||||
if (sz)
|
||||
*sz = size;
|
||||
|
||||
return (ptr);
|
||||
return ((char *) ptr);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,204 +24,212 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_scn.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
/*
|
||||
* Load an ELF section table and create a list of Elf_Scn structures.
|
||||
*/
|
||||
static int
|
||||
_libelf_load_scn(Elf *e, void *ehdr)
|
||||
int
|
||||
_libelf_load_section_headers(Elf *e, void *ehdr)
|
||||
{
|
||||
int ec, swapbytes;
|
||||
size_t fsz, i, shnum;
|
||||
uint64_t shoff;
|
||||
char *src;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
Elf_Scn *scn;
|
||||
void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
|
||||
Elf_Scn *scn;
|
||||
uint64_t shoff;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
int ec, swapbytes;
|
||||
unsigned char *src;
|
||||
size_t fsz, i, shnum;
|
||||
int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
|
||||
size_t _c, int _swap);
|
||||
|
||||
assert(e != NULL);
|
||||
assert(ehdr != NULL);
|
||||
assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
|
||||
assert(e != NULL);
|
||||
assert(ehdr != NULL);
|
||||
assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
|
||||
|
||||
#define CHECK_EHDR(E,EH) do { \
|
||||
if (fsz != (EH)->e_shentsize || \
|
||||
shoff + fsz * shnum > e->e_rawsize) { \
|
||||
LIBELF_SET_ERROR(HEADER, 0); \
|
||||
return (0); \
|
||||
} \
|
||||
} while (0)
|
||||
if (shoff > e->e_rawsize || \
|
||||
fsz != (EH)->e_shentsize || \
|
||||
shnum > SIZE_MAX / fsz || \
|
||||
fsz * shnum > e->e_rawsize - shoff) { \
|
||||
LIBELF_SET_ERROR(HEADER, 0); \
|
||||
return (0); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
ec = e->e_class;
|
||||
fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
|
||||
assert(fsz > 0);
|
||||
ec = e->e_class;
|
||||
fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
|
||||
assert(fsz > 0);
|
||||
|
||||
shnum = e->e_u.e_elf.e_nscn;
|
||||
shnum = e->e_u.e_elf.e_nscn;
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
eh32 = (Elf32_Ehdr *) ehdr;
|
||||
shoff = (uint64_t) eh32->e_shoff;
|
||||
CHECK_EHDR(e, eh32);
|
||||
} else {
|
||||
eh64 = (Elf64_Ehdr *) ehdr;
|
||||
shoff = eh64->e_shoff;
|
||||
CHECK_EHDR(e, eh64);
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
eh32 = (Elf32_Ehdr *) ehdr;
|
||||
shoff = (uint64_t) eh32->e_shoff;
|
||||
CHECK_EHDR(e, eh32);
|
||||
} else {
|
||||
eh64 = (Elf64_Ehdr *) ehdr;
|
||||
shoff = eh64->e_shoff;
|
||||
CHECK_EHDR(e, eh64);
|
||||
}
|
||||
|
||||
xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
|
||||
xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
|
||||
|
||||
swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
|
||||
src = e->e_rawfile + shoff;
|
||||
swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
|
||||
src = e->e_rawfile + shoff;
|
||||
|
||||
/*
|
||||
* If the file is using extended numbering then section #0
|
||||
* would have already been read in.
|
||||
*/
|
||||
/*
|
||||
* If the file is using extended numbering then section #0
|
||||
* would have already been read in.
|
||||
*/
|
||||
|
||||
i = 0;
|
||||
if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
|
||||
assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==
|
||||
STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next));
|
||||
i = 0;
|
||||
if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
|
||||
assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==
|
||||
STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next));
|
||||
|
||||
i = 1;
|
||||
src += fsz;
|
||||
}
|
||||
i = 1;
|
||||
src += fsz;
|
||||
}
|
||||
|
||||
for (; i < shnum; i++, src += fsz) {
|
||||
if ((scn = _libelf_allocate_scn(e, i)) == NULL)
|
||||
return (0);
|
||||
for (; i < shnum; i++, src += fsz) {
|
||||
if ((scn = _libelf_allocate_scn(e, i)) == NULL)
|
||||
return (0);
|
||||
|
||||
(*xlator)((char *) &scn->s_shdr, src, (size_t) 1, swapbytes);
|
||||
(*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
|
||||
src, (size_t) 1, swapbytes);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
scn->s_offset = scn->s_rawoff =
|
||||
scn->s_shdr.s_shdr32.sh_offset;
|
||||
scn->s_size = scn->s_shdr.s_shdr32.sh_size;
|
||||
} else {
|
||||
scn->s_offset = scn->s_rawoff =
|
||||
scn->s_shdr.s_shdr64.sh_offset;
|
||||
scn->s_size = scn->s_shdr.s_shdr64.sh_size;
|
||||
}
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
scn->s_offset = scn->s_rawoff =
|
||||
scn->s_shdr.s_shdr32.sh_offset;
|
||||
scn->s_size = scn->s_shdr.s_shdr32.sh_size;
|
||||
} else {
|
||||
scn->s_offset = scn->s_rawoff =
|
||||
scn->s_shdr.s_shdr64.sh_offset;
|
||||
scn->s_size = scn->s_shdr.s_shdr64.sh_size;
|
||||
}
|
||||
}
|
||||
|
||||
e->e_flags |= LIBELF_F_SHDRS_LOADED;
|
||||
e->e_flags |= LIBELF_F_SHDRS_LOADED;
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
Elf_Scn *
|
||||
elf_getscn(Elf *e, size_t index)
|
||||
{
|
||||
int ec;
|
||||
void *ehdr;
|
||||
Elf_Scn *s;
|
||||
int ec;
|
||||
void *ehdr;
|
||||
Elf_Scn *s;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (NULL);
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (e->e_cmd != ELF_C_WRITE &&
|
||||
(e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
|
||||
_libelf_load_scn(e, ehdr) == 0)
|
||||
return (NULL);
|
||||
if (e->e_cmd != ELF_C_WRITE &&
|
||||
(e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
|
||||
_libelf_load_section_headers(e, ehdr) == 0)
|
||||
return (NULL);
|
||||
|
||||
STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
|
||||
if (s->s_ndx == index)
|
||||
return (s);
|
||||
STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
|
||||
if (s->s_ndx == index)
|
||||
return (s);
|
||||
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
size_t
|
||||
elf_ndxscn(Elf_Scn *s)
|
||||
{
|
||||
if (s == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (SHN_UNDEF);
|
||||
}
|
||||
return (s->s_ndx);
|
||||
if (s == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (SHN_UNDEF);
|
||||
}
|
||||
return (s->s_ndx);
|
||||
}
|
||||
|
||||
Elf_Scn *
|
||||
elf_newscn(Elf *e)
|
||||
{
|
||||
int ec;
|
||||
void *ehdr;
|
||||
Elf_Scn *scn;
|
||||
int ec;
|
||||
void *ehdr;
|
||||
Elf_Scn *scn;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (NULL);
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
/*
|
||||
* The application may be asking for a new section descriptor
|
||||
* on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
|
||||
* need to bring in the existing section information before
|
||||
* appending a new one to the list.
|
||||
*
|
||||
* Per the ELF(3) API, an application is allowed to open a
|
||||
* file using ELF_C_READ, mess with its internal structure and
|
||||
* use elf_update(...,ELF_C_NULL) to compute its new layout.
|
||||
*/
|
||||
if (e->e_cmd != ELF_C_WRITE &&
|
||||
(e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
|
||||
_libelf_load_scn(e, ehdr) == 0)
|
||||
return (NULL);
|
||||
/*
|
||||
* The application may be asking for a new section descriptor
|
||||
* on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We
|
||||
* need to bring in the existing section information before
|
||||
* appending a new one to the list.
|
||||
*
|
||||
* Per the ELF(3) API, an application is allowed to open a
|
||||
* file using ELF_C_READ, mess with its internal structure and
|
||||
* use elf_update(...,ELF_C_NULL) to compute its new layout.
|
||||
*/
|
||||
if (e->e_cmd != ELF_C_WRITE &&
|
||||
(e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
|
||||
_libelf_load_section_headers(e, ehdr) == 0)
|
||||
return (NULL);
|
||||
|
||||
if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
|
||||
assert(e->e_u.e_elf.e_nscn == 0);
|
||||
if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
|
||||
NULL)
|
||||
return (NULL);
|
||||
e->e_u.e_elf.e_nscn++;
|
||||
}
|
||||
if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
|
||||
assert(e->e_u.e_elf.e_nscn == 0);
|
||||
if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
|
||||
NULL)
|
||||
return (NULL);
|
||||
e->e_u.e_elf.e_nscn++;
|
||||
}
|
||||
|
||||
assert(e->e_u.e_elf.e_nscn > 0);
|
||||
assert(e->e_u.e_elf.e_nscn > 0);
|
||||
|
||||
if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
|
||||
return (NULL);
|
||||
if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
e->e_u.e_elf.e_nscn++;
|
||||
e->e_u.e_elf.e_nscn++;
|
||||
|
||||
(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
return (scn);
|
||||
return (scn);
|
||||
}
|
||||
|
||||
Elf_Scn *
|
||||
elf_nextscn(Elf *e, Elf_Scn *s)
|
||||
{
|
||||
if (e == NULL || (e->e_kind != ELF_K_ELF) ||
|
||||
(s && s->s_elf != e)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL || (e->e_kind != ELF_K_ELF) ||
|
||||
(s && s->s_elf != e)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (s == NULL ? elf_getscn(e, (size_t) 1) :
|
||||
STAILQ_NEXT(s, s_next));
|
||||
return (s == NULL ? elf_getscn(e, (size_t) 1) :
|
||||
STAILQ_NEXT(s, s_next));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,28 +24,42 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_shnum.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
static int
|
||||
_libelf_getshdrnum(Elf *e, size_t *shnum)
|
||||
{
|
||||
void *eh;
|
||||
int ec;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (-1);
|
||||
|
||||
*shnum = e->e_u.e_elf.e_nscn;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
elf_getshdrnum(Elf *e, size_t *shnum)
|
||||
{
|
||||
return (_libelf_getshdrnum(e, shnum));
|
||||
}
|
||||
|
||||
/* Deprecated API. */
|
||||
int
|
||||
elf_getshnum(Elf *e, size_t *shnum)
|
||||
{
|
||||
void *eh;
|
||||
int ec;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
|
||||
*shnum = e->e_u.e_elf.e_nscn;
|
||||
|
||||
return (1);
|
||||
return (_libelf_getshdrnum(e, shnum) >= 0);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,44 +24,57 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ar.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
int
|
||||
elf_getshstrndx(Elf *e, size_t *strndx)
|
||||
ELFTC_VCSID("$Id: elf_shstrndx.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
static int
|
||||
_libelf_getshdrstrndx(Elf *e, size_t *strndx)
|
||||
{
|
||||
void *eh;
|
||||
int ec;
|
||||
void *eh;
|
||||
int ec;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (-1);
|
||||
|
||||
*strndx = e->e_u.e_elf.e_strndx;
|
||||
*strndx = e->e_u.e_elf.e_strndx;
|
||||
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
elf_getshdrstrndx(Elf *e, size_t *strndx)
|
||||
{
|
||||
return (_libelf_getshdrstrndx(e, strndx));
|
||||
}
|
||||
|
||||
int
|
||||
elf_getshstrndx(Elf *e, size_t *strndx) /* Deprecated API. */
|
||||
{
|
||||
return (_libelf_getshdrstrndx(e, strndx) >= 0);
|
||||
}
|
||||
|
||||
int
|
||||
elf_setshstrndx(Elf *e, size_t strndx)
|
||||
{
|
||||
void *eh;
|
||||
int ec;
|
||||
void *eh;
|
||||
int ec;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
|
||||
((eh = _libelf_ehdr(e, ec, 0)) == NULL)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
|
||||
((eh = _libelf_ehdr(e, ec, 0)) == NULL)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
return (_libelf_setshstrndx(e, eh, ec, strndx));
|
||||
return (_libelf_setshstrndx(e, eh, ec, strndx));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,17 +24,15 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/param.h>
|
||||
#ifdef __sun
|
||||
#include <sys/sysmacros.h>
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_strptr.c 2990 2014-03-17 09:56:58Z jkoshy $");
|
||||
|
||||
/*
|
||||
* Convert an ELF section#,offset pair to a string pointer.
|
||||
*/
|
||||
@@ -42,93 +40,93 @@
|
||||
char *
|
||||
elf_strptr(Elf *e, size_t scndx, size_t offset)
|
||||
{
|
||||
Elf_Scn *s;
|
||||
Elf_Data *d;
|
||||
size_t alignment, count;
|
||||
GElf_Shdr shdr;
|
||||
Elf_Scn *s;
|
||||
Elf_Data *d;
|
||||
GElf_Shdr shdr;
|
||||
uint64_t alignment, count;
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((s = elf_getscn(e, scndx)) == NULL ||
|
||||
gelf_getshdr(s, &shdr) == NULL)
|
||||
return (NULL);
|
||||
if ((s = elf_getscn(e, scndx)) == NULL ||
|
||||
gelf_getshdr(s, &shdr) == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (shdr.sh_type != SHT_STRTAB ||
|
||||
offset >= shdr.sh_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (shdr.sh_type != SHT_STRTAB ||
|
||||
offset >= shdr.sh_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
d = NULL;
|
||||
if (e->e_flags & ELF_F_LAYOUT) {
|
||||
d = NULL;
|
||||
if (e->e_flags & ELF_F_LAYOUT) {
|
||||
|
||||
/*
|
||||
* The application is taking responsibility for the
|
||||
* ELF object's layout, so we can directly translate
|
||||
* an offset to a `char *' address using the `d_off'
|
||||
* members of Elf_Data descriptors.
|
||||
*/
|
||||
while ((d = elf_getdata(s, d)) != NULL) {
|
||||
/*
|
||||
* The application is taking responsibility for the
|
||||
* ELF object's layout, so we can directly translate
|
||||
* an offset to a `char *' address using the `d_off'
|
||||
* members of Elf_Data descriptors.
|
||||
*/
|
||||
while ((d = elf_getdata(s, d)) != NULL) {
|
||||
|
||||
if (d->d_buf == 0 || d->d_size == 0)
|
||||
continue;
|
||||
if (d->d_buf == 0 || d->d_size == 0)
|
||||
continue;
|
||||
|
||||
if (d->d_type != ELF_T_BYTE) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (d->d_type != ELF_T_BYTE) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (offset >= d->d_off &&
|
||||
offset < d->d_off + d->d_size)
|
||||
return ((char *) d->d_buf + offset - d->d_off);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Otherwise, the `d_off' members are not useable and
|
||||
* we need to compute offsets ourselves, taking into
|
||||
* account 'holes' in coverage of the section introduced
|
||||
* by alignment requirements.
|
||||
*/
|
||||
count = (size_t) 0; /* cumulative count of bytes seen */
|
||||
while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
|
||||
if (offset >= d->d_off &&
|
||||
offset < d->d_off + d->d_size)
|
||||
return ((char *) d->d_buf + offset - d->d_off);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Otherwise, the `d_off' members are not useable and
|
||||
* we need to compute offsets ourselves, taking into
|
||||
* account 'holes' in coverage of the section introduced
|
||||
* by alignment requirements.
|
||||
*/
|
||||
count = (uint64_t) 0; /* cumulative count of bytes seen */
|
||||
while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
|
||||
|
||||
if (d->d_buf == NULL || d->d_size == 0)
|
||||
continue;
|
||||
if (d->d_buf == NULL || d->d_size == 0)
|
||||
continue;
|
||||
|
||||
if (d->d_type != ELF_T_BYTE) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (d->d_type != ELF_T_BYTE) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((alignment = d->d_align) > 1) {
|
||||
if ((alignment & (alignment - 1)) != 0) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
count = roundup(count, alignment);
|
||||
}
|
||||
if ((alignment = d->d_align) > 1) {
|
||||
if ((alignment & (alignment - 1)) != 0) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
count = roundup2(count, alignment);
|
||||
}
|
||||
|
||||
if (offset < count) {
|
||||
/* offset starts in the 'hole' */
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (offset < count) {
|
||||
/* offset starts in the 'hole' */
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (offset < count + d->d_size) {
|
||||
if (d->d_buf != NULL)
|
||||
return ((char *) d->d_buf +
|
||||
offset - count);
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (offset < count + d->d_size) {
|
||||
if (d->d_buf != NULL)
|
||||
return ((char *) d->d_buf +
|
||||
offset - count);
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
count += d->d_size;
|
||||
}
|
||||
}
|
||||
count += d->d_size;
|
||||
}
|
||||
}
|
||||
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -23,44 +23,43 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/elf_types.m4,v 1.2 2006/12/18 05:40:01 jkoshy Exp $
|
||||
* $Id: elf_types.m4 321 2009-03-07 16:59:14Z jkoshy $
|
||||
*/
|
||||
|
||||
/*
|
||||
* ELF types, defined in the "enum Elf_Type" API.
|
||||
*
|
||||
* The members of the list form a 3-tuple: (name, C-type-suffix, OSversion).
|
||||
* The members of the list form a 2-tuple: (name, C-type-suffix).
|
||||
* + `name' is an Elf_Type symbol without the `ELF_T_' prefix.
|
||||
* + `C-type-suffix' is the suffix for Elf32_ and Elf64_ type names.
|
||||
* + `version' is the OS version the symbol first appeared in.
|
||||
*
|
||||
*/
|
||||
|
||||
define(`ELF_TYPE_LIST',
|
||||
``ADDR, Addr, 600102',
|
||||
`BYTE, Byte, 600102',
|
||||
`CAP, Cap, 700025',
|
||||
`DYN, Dyn, 600102',
|
||||
`EHDR, Ehdr, 600102',
|
||||
`HALF, Half, 600102',
|
||||
`LWORD, Lword, 700025',
|
||||
`MOVE, Move, 700025',
|
||||
`MOVEP, MoveP, 700025',
|
||||
`NOTE, Note, 600102',
|
||||
`OFF, Off, 600102',
|
||||
`PHDR, Phdr, 600102',
|
||||
`REL, Rel, 600102',
|
||||
`RELA, Rela, 600102',
|
||||
`SHDR, Shdr, 600102',
|
||||
`SWORD, Sword, 600102',
|
||||
`SXWORD, Sxword, 700009',
|
||||
`SYMINFO, Syminfo, 700025',
|
||||
`SYM, Sym, 600102',
|
||||
`VDEF, Verdef, 700009',
|
||||
`VNEED, Verneed, 700009',
|
||||
`WORD, Word, 600102',
|
||||
`XWORD, Xword, 700009',
|
||||
`NUM, _, _'')
|
||||
``ADDR, Addr',
|
||||
`BYTE, Byte',
|
||||
`CAP, Cap',
|
||||
`DYN, Dyn',
|
||||
`EHDR, Ehdr',
|
||||
`GNUHASH, -',
|
||||
`HALF, Half',
|
||||
`LWORD, Lword',
|
||||
`MOVE, Move',
|
||||
`MOVEP, MoveP',
|
||||
`NOTE, Note',
|
||||
`OFF, Off',
|
||||
`PHDR, Phdr',
|
||||
`REL, Rel',
|
||||
`RELA, Rela',
|
||||
`SHDR, Shdr',
|
||||
`SWORD, Sword',
|
||||
`SXWORD, Sxword',
|
||||
`SYMINFO, Syminfo',
|
||||
`SYM, Sym',
|
||||
`VDEF, Verdef',
|
||||
`VNEED, Verneed',
|
||||
`WORD, Word',
|
||||
`XWORD, Xword',
|
||||
`NUM, _'')
|
||||
|
||||
/*
|
||||
* DEFINE_STRUCT(NAME,MEMBERLIST...)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,24 +24,27 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: elf_version.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
unsigned int
|
||||
elf_version(unsigned int v)
|
||||
{
|
||||
unsigned int old;
|
||||
unsigned int old;
|
||||
|
||||
if ((old = LIBELF_PRIVATE(version)) == EV_NONE)
|
||||
old = EV_CURRENT;
|
||||
if ((old = LIBELF_PRIVATE(version)) == EV_NONE)
|
||||
old = EV_CURRENT;
|
||||
|
||||
if (v == EV_NONE)
|
||||
return old;
|
||||
if (v > EV_CURRENT) {
|
||||
LIBELF_SET_ERROR(VERSION, 0);
|
||||
return EV_NONE;
|
||||
}
|
||||
if (v == EV_NONE)
|
||||
return old;
|
||||
if (v > EV_CURRENT) {
|
||||
LIBELF_SET_ERROR(VERSION, 0);
|
||||
return EV_NONE;
|
||||
}
|
||||
|
||||
LIBELF_PRIVATE(version) = v;
|
||||
return (old);
|
||||
LIBELF_PRIVATE(version) = v;
|
||||
return (old);
|
||||
}
|
||||
|
||||
2701
ext/libelf/elfdefinitions.h
Normal file
2701
ext/libelf/elfdefinitions.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -23,18 +23,13 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/gelf.h,v 1.3 2007/03/08 04:01:30 jkoshy Exp $
|
||||
* $Id: gelf.h 3174 2015-03-27 17:13:41Z emaste $
|
||||
*/
|
||||
|
||||
#ifndef _GELF_H_
|
||||
#define _GELF_H_
|
||||
|
||||
|
||||
#include "libelf.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <libelf.h>
|
||||
|
||||
typedef Elf64_Addr GElf_Addr; /* Addresses */
|
||||
typedef Elf64_Half GElf_Half; /* Half words (16 bit) */
|
||||
@@ -72,9 +67,12 @@ typedef Elf64_Syminfo GElf_Syminfo; /* Symbol information */
|
||||
#define GELF_ST_TYPE ELF64_ST_TYPE
|
||||
#define GELF_ST_VISIBILITY ELF64_ST_VISIBILITY
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
long gelf_checksum(Elf *_elf);
|
||||
size_t gelf_fsize(Elf *_elf, Elf_Type _type, size_t _count,
|
||||
unsigned int _version);
|
||||
unsigned int _version);
|
||||
int gelf_getclass(Elf *_elf);
|
||||
GElf_Dyn *gelf_getdyn(Elf_Data *_data, int _index, GElf_Dyn *_dst);
|
||||
GElf_Ehdr *gelf_getehdr(Elf *_elf, GElf_Ehdr *_dst);
|
||||
@@ -84,7 +82,7 @@ GElf_Rela *gelf_getrela(Elf_Data *_src, int _index, GElf_Rela *_dst);
|
||||
GElf_Shdr *gelf_getshdr(Elf_Scn *_scn, GElf_Shdr *_dst);
|
||||
GElf_Sym *gelf_getsym(Elf_Data *_src, int _index, GElf_Sym *_dst);
|
||||
GElf_Sym *gelf_getsymshndx(Elf_Data *_src, Elf_Data *_shindexsrc,
|
||||
int _index, GElf_Sym *_dst, Elf32_Word *_shindexdst);
|
||||
int _index, GElf_Sym *_dst, Elf32_Word *_shindexdst);
|
||||
void * gelf_newehdr(Elf *_elf, int _class);
|
||||
void * gelf_newphdr(Elf *_elf, size_t _phnum);
|
||||
int gelf_update_dyn(Elf_Data *_dst, int _index, GElf_Dyn *_src);
|
||||
@@ -95,7 +93,7 @@ int gelf_update_rela(Elf_Data *_dst, int _index, GElf_Rela *_src);
|
||||
int gelf_update_shdr(Elf_Scn *_dst, GElf_Shdr *_src);
|
||||
int gelf_update_sym(Elf_Data *_dst, int _index, GElf_Sym *_src);
|
||||
int gelf_update_symshndx(Elf_Data *_symdst, Elf_Data *_shindexdst,
|
||||
int _index, GElf_Sym *_symsrc, Elf32_Word _shindexsrc);
|
||||
int _index, GElf_Sym *_symsrc, Elf32_Word _shindexsrc);
|
||||
Elf_Data *gelf_xlatetof(Elf *_elf, Elf_Data *_dst, const Elf_Data *_src, unsigned int _encode);
|
||||
Elf_Data *gelf_xlatetom(Elf *_elf, Elf_Data *_dst, const Elf_Data *_src, unsigned int _encode);
|
||||
|
||||
@@ -105,7 +103,6 @@ GElf_Syminfo *gelf_getsyminfo(Elf_Data *_src, int _index, GElf_Syminfo *_dst);
|
||||
int gelf_update_cap(Elf_Data *_dst, int _index, GElf_Cap *_src);
|
||||
int gelf_update_move(Elf_Data *_dst, int _index, GElf_Move *_src);
|
||||
int gelf_update_syminfo(Elf_Data *_dst, int _index, GElf_Syminfo *_src);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
150
ext/libelf/gelf_cap.c
Normal file
150
ext/libelf/gelf_cap.c
Normal file
@@ -0,0 +1,150 @@
|
||||
/*-
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <gelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_cap.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
GElf_Cap *
|
||||
gelf_getcap(Elf_Data *ed, int ndx, GElf_Cap *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Cap *cap32;
|
||||
Elf64_Cap *cap64;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_CAP, ec, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
|
||||
cap32 = (Elf32_Cap *) d->d_data.d_buf + ndx;
|
||||
|
||||
dst->c_tag = cap32->c_tag;
|
||||
dst->c_un.c_val = (Elf64_Xword) cap32->c_un.c_val;
|
||||
|
||||
} else {
|
||||
|
||||
cap64 = (Elf64_Cap *) d->d_data.d_buf + ndx;
|
||||
|
||||
*dst = *cap64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_cap(Elf_Data *ed, int ndx, GElf_Cap *gc)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Cap *cap32;
|
||||
Elf64_Cap *cap64;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (d == NULL || ndx < 0 || gc == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_CAP, ec, e->e_version);
|
||||
assert(msz > 0);
|
||||
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
cap32 = (Elf32_Cap *) d->d_data.d_buf + ndx;
|
||||
|
||||
LIBELF_COPY_U32(cap32, gc, c_tag);
|
||||
LIBELF_COPY_U32(cap32, gc, c_un.c_val);
|
||||
} else {
|
||||
cap64 = (Elf64_Cap *) d->d_data.d_buf + ndx;
|
||||
|
||||
*cap64 = *gc;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,31 +24,33 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_checksum.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
long
|
||||
elf32_checksum(Elf *e)
|
||||
{
|
||||
return (_libelf_checksum(e, ELFCLASS32));
|
||||
return (_libelf_checksum(e, ELFCLASS32));
|
||||
}
|
||||
|
||||
long
|
||||
elf64_checksum(Elf *e)
|
||||
{
|
||||
return (_libelf_checksum(e, ELFCLASS64));
|
||||
return (_libelf_checksum(e, ELFCLASS64));
|
||||
}
|
||||
|
||||
long
|
||||
gelf_checksum(Elf *e)
|
||||
{
|
||||
int ec;
|
||||
if (e == NULL ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0L);
|
||||
}
|
||||
return (_libelf_checksum(e, ec));
|
||||
int ec;
|
||||
if (e == NULL ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0L);
|
||||
}
|
||||
return (_libelf_checksum(e, ec));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -25,116 +25,128 @@
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <gelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "gelf.h"
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_dyn.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
GElf_Dyn *
|
||||
gelf_getdyn(Elf_Data *d, int ndx, GElf_Dyn *dst)
|
||||
gelf_getdyn(Elf_Data *ed, int ndx, GElf_Dyn *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Dyn *dyn32;
|
||||
Elf64_Dyn *dyn64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Dyn *dyn32;
|
||||
Elf64_Dyn *dyn64;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_DYN, ec, e->e_version);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(msz > 0);
|
||||
msz = _libelf_msize(ELF_T_DYN, ec, e->e_version);
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
dyn32 = (Elf32_Dyn *) d->d_buf + ndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
dst->d_tag = dyn32->d_tag;
|
||||
dst->d_un.d_val = (Elf64_Xword) dyn32->d_un.d_val;
|
||||
if (ec == ELFCLASS32) {
|
||||
dyn32 = (Elf32_Dyn *) d->d_data.d_buf + ndx;
|
||||
|
||||
} else {
|
||||
dst->d_tag = dyn32->d_tag;
|
||||
dst->d_un.d_val = (Elf64_Xword) dyn32->d_un.d_val;
|
||||
|
||||
dyn64 = (Elf64_Dyn *) d->d_buf + ndx;
|
||||
} else {
|
||||
|
||||
*dst = *dyn64;
|
||||
}
|
||||
dyn64 = (Elf64_Dyn *) d->d_data.d_buf + ndx;
|
||||
|
||||
return (dst);
|
||||
*dst = *dyn64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_dyn(Elf_Data *d, int ndx, GElf_Dyn *ds)
|
||||
gelf_update_dyn(Elf_Data *ed, int ndx, GElf_Dyn *ds)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Dyn *dyn32;
|
||||
Elf64_Dyn *dyn64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Dyn *dyn32;
|
||||
Elf64_Dyn *dyn64;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || ds == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || ds == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_DYN, ec, e->e_version);
|
||||
assert(msz > 0);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
msz = _libelf_msize(ELF_T_DYN, ec, e->e_version);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
dyn32 = (Elf32_Dyn *) d->d_buf + ndx;
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
LIBELF_COPY_S32(dyn32, ds, d_tag);
|
||||
LIBELF_COPY_U32(dyn32, ds, d_un.d_val);
|
||||
} else {
|
||||
dyn64 = (Elf64_Dyn *) d->d_buf + ndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
*dyn64 = *ds;
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
dyn32 = (Elf32_Dyn *) d->d_data.d_buf + ndx;
|
||||
|
||||
return (1);
|
||||
LIBELF_COPY_S32(dyn32, ds, d_tag);
|
||||
LIBELF_COPY_U32(dyn32, ds, d_un.d_val);
|
||||
} else {
|
||||
dyn64 = (Elf64_Dyn *) d->d_data.d_buf + ndx;
|
||||
|
||||
*dyn64 = *ds;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,141 +24,144 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_ehdr.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
Elf32_Ehdr *
|
||||
elf32_getehdr(Elf *e)
|
||||
{
|
||||
return (_libelf_ehdr(e, ELFCLASS32, 0));
|
||||
return (_libelf_ehdr(e, ELFCLASS32, 0));
|
||||
}
|
||||
|
||||
Elf64_Ehdr *
|
||||
elf64_getehdr(Elf *e)
|
||||
{
|
||||
return (_libelf_ehdr(e, ELFCLASS64, 0));
|
||||
return (_libelf_ehdr(e, ELFCLASS64, 0));
|
||||
}
|
||||
|
||||
GElf_Ehdr *
|
||||
gelf_getehdr(Elf *e, GElf_Ehdr *d)
|
||||
{
|
||||
int ec;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
int ec;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
|
||||
if (d == NULL || e == NULL ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (d == NULL || e == NULL ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL)
|
||||
return (NULL);
|
||||
if (ec == ELFCLASS32) {
|
||||
if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
(void) memcpy(d->e_ident, eh32->e_ident, sizeof(eh32->e_ident));
|
||||
d->e_type = eh32->e_type;
|
||||
d->e_machine = eh32->e_machine;
|
||||
d->e_version = eh32->e_version;
|
||||
d->e_entry = eh32->e_entry;
|
||||
d->e_phoff = eh32->e_phoff;
|
||||
d->e_shoff = eh32->e_shoff;
|
||||
d->e_flags = eh32->e_flags;
|
||||
d->e_ehsize = eh32->e_ehsize;
|
||||
d->e_phentsize = eh32->e_phentsize;
|
||||
d->e_phnum = eh32->e_phnum;
|
||||
d->e_shentsize = eh32->e_shentsize;
|
||||
d->e_shnum = eh32->e_shnum;
|
||||
d->e_shstrndx = eh32->e_shstrndx;
|
||||
(void) memcpy(d->e_ident, eh32->e_ident,
|
||||
sizeof(eh32->e_ident));
|
||||
d->e_type = eh32->e_type;
|
||||
d->e_machine = eh32->e_machine;
|
||||
d->e_version = eh32->e_version;
|
||||
d->e_entry = eh32->e_entry;
|
||||
d->e_phoff = eh32->e_phoff;
|
||||
d->e_shoff = eh32->e_shoff;
|
||||
d->e_flags = eh32->e_flags;
|
||||
d->e_ehsize = eh32->e_ehsize;
|
||||
d->e_phentsize = eh32->e_phentsize;
|
||||
d->e_phnum = eh32->e_phnum;
|
||||
d->e_shentsize = eh32->e_shentsize;
|
||||
d->e_shnum = eh32->e_shnum;
|
||||
d->e_shstrndx = eh32->e_shstrndx;
|
||||
|
||||
return (d);
|
||||
}
|
||||
return (d);
|
||||
}
|
||||
|
||||
assert(ec == ELFCLASS64);
|
||||
assert(ec == ELFCLASS64);
|
||||
|
||||
if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL)
|
||||
return (NULL);
|
||||
*d = *eh64;
|
||||
if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL)
|
||||
return (NULL);
|
||||
*d = *eh64;
|
||||
|
||||
return (d);
|
||||
return (d);
|
||||
}
|
||||
|
||||
Elf32_Ehdr *
|
||||
elf32_newehdr(Elf *e)
|
||||
{
|
||||
return (_libelf_ehdr(e, ELFCLASS32, 1));
|
||||
return (_libelf_ehdr(e, ELFCLASS32, 1));
|
||||
}
|
||||
|
||||
Elf64_Ehdr *
|
||||
elf64_newehdr(Elf *e)
|
||||
{
|
||||
return (_libelf_ehdr(e, ELFCLASS64, 1));
|
||||
return (_libelf_ehdr(e, ELFCLASS64, 1));
|
||||
}
|
||||
|
||||
void *
|
||||
gelf_newehdr(Elf *e, int ec)
|
||||
{
|
||||
if (e != NULL &&
|
||||
(ec == ELFCLASS32 || ec == ELFCLASS64))
|
||||
return (_libelf_ehdr(e, ec, 1));
|
||||
if (e != NULL &&
|
||||
(ec == ELFCLASS32 || ec == ELFCLASS64))
|
||||
return (_libelf_ehdr(e, ec, 1));
|
||||
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_ehdr(Elf *e, GElf_Ehdr *s)
|
||||
{
|
||||
int ec;
|
||||
void *ehdr;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
int ec;
|
||||
void *ehdr;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
|
||||
if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (e->e_cmd == ELF_C_READ) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e->e_cmd == ELF_C_READ) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
|
||||
if (ec == ELFCLASS64) {
|
||||
eh64 = (Elf64_Ehdr *) ehdr;
|
||||
*eh64 = *s;
|
||||
return (1);
|
||||
}
|
||||
(void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
eh32 = (Elf32_Ehdr *) ehdr;
|
||||
if (ec == ELFCLASS64) {
|
||||
eh64 = (Elf64_Ehdr *) ehdr;
|
||||
*eh64 = *s;
|
||||
return (1);
|
||||
}
|
||||
|
||||
(void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident));
|
||||
eh32 = (Elf32_Ehdr *) ehdr;
|
||||
|
||||
eh32->e_type = s->e_type;
|
||||
eh32->e_machine = s->e_machine;
|
||||
eh32->e_version = s->e_version;
|
||||
LIBELF_COPY_U32(eh32, s, e_entry);
|
||||
LIBELF_COPY_U32(eh32, s, e_phoff);
|
||||
LIBELF_COPY_U32(eh32, s, e_shoff);
|
||||
eh32->e_flags = s->e_flags;
|
||||
eh32->e_ehsize = s->e_ehsize;
|
||||
eh32->e_phentsize = s->e_phentsize;
|
||||
eh32->e_phnum = s->e_phnum;
|
||||
eh32->e_shentsize = s->e_shentsize;
|
||||
eh32->e_shnum = s->e_shnum;
|
||||
eh32->e_shstrndx = s->e_shstrndx;
|
||||
(void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident));
|
||||
|
||||
(void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
|
||||
eh32->e_type = s->e_type;
|
||||
eh32->e_machine = s->e_machine;
|
||||
eh32->e_version = s->e_version;
|
||||
LIBELF_COPY_U32(eh32, s, e_entry);
|
||||
LIBELF_COPY_U32(eh32, s, e_phoff);
|
||||
LIBELF_COPY_U32(eh32, s, e_shoff);
|
||||
eh32->e_flags = s->e_flags;
|
||||
eh32->e_ehsize = s->e_ehsize;
|
||||
eh32->e_phentsize = s->e_phentsize;
|
||||
eh32->e_phnum = s->e_phnum;
|
||||
eh32->e_shentsize = s->e_shentsize;
|
||||
eh32->e_shnum = s->e_shnum;
|
||||
eh32->e_shstrndx = s->e_shstrndx;
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,35 +24,37 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_fsize.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
size_t
|
||||
elf32_fsize(Elf_Type t, size_t c, unsigned int v)
|
||||
{
|
||||
return (_libelf_fsize(t, ELFCLASS32, v, c));
|
||||
return (_libelf_fsize(t, ELFCLASS32, v, c));
|
||||
}
|
||||
|
||||
size_t
|
||||
elf64_fsize(Elf_Type t, size_t c, unsigned int v)
|
||||
{
|
||||
return (_libelf_fsize(t, ELFCLASS64, v, c));
|
||||
return (_libelf_fsize(t, ELFCLASS64, v, c));
|
||||
}
|
||||
|
||||
size_t
|
||||
gelf_fsize(Elf *e, Elf_Type t, size_t c, unsigned int v)
|
||||
{
|
||||
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (e->e_class == ELFCLASS32 || e->e_class == ELFCLASS64)
|
||||
return (_libelf_fsize(t, e->e_class, v, c));
|
||||
if (e->e_class == ELFCLASS32 || e->e_class == ELFCLASS64)
|
||||
return (_libelf_fsize(t, e->e_class, v, c));
|
||||
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,12 +24,14 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_getclass.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
int
|
||||
gelf_getclass(Elf *e)
|
||||
{
|
||||
return (e != NULL ? e->e_class : ELFCLASSNONE);
|
||||
return (e != NULL ? e->e_class : ELFCLASSNONE);
|
||||
}
|
||||
|
||||
159
ext/libelf/gelf_move.c
Normal file
159
ext/libelf/gelf_move.c
Normal file
@@ -0,0 +1,159 @@
|
||||
/*-
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <gelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_move.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
GElf_Move *
|
||||
gelf_getmove(Elf_Data *ed, int ndx, GElf_Move *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Move *move32;
|
||||
Elf64_Move *move64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
|
||||
move32 = (Elf32_Move *) d->d_data.d_buf + ndx;
|
||||
|
||||
dst->m_value = move32->m_value;
|
||||
dst->m_info = (Elf64_Xword) move32->m_info;
|
||||
dst->m_poffset = (Elf64_Xword) move32->m_poffset;
|
||||
dst->m_repeat = move32->m_repeat;
|
||||
dst->m_stride = move32->m_stride;
|
||||
} else {
|
||||
|
||||
move64 = (Elf64_Move *) d->d_data.d_buf + ndx;
|
||||
|
||||
*dst = *move64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_move(Elf_Data *ed, int ndx, GElf_Move *gm)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Move *move32;
|
||||
Elf64_Move *move64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (d == NULL || ndx < 0 || gm == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
move32 = (Elf32_Move *) d->d_data.d_buf + ndx;
|
||||
|
||||
move32->m_value = gm->m_value;
|
||||
LIBELF_COPY_U32(move32, gm, m_info);
|
||||
LIBELF_COPY_U32(move32, gm, m_poffset);
|
||||
move32->m_repeat = gm->m_repeat;
|
||||
move32->m_stride = gm->m_stride;
|
||||
|
||||
} else {
|
||||
move64 = (Elf64_Move *) d->d_data.d_buf + ndx;
|
||||
|
||||
*move64 = *gm;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,152 +24,154 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_phdr.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
Elf32_Phdr *
|
||||
elf32_getphdr(Elf *e)
|
||||
{
|
||||
return (_libelf_getphdr(e, ELFCLASS32));
|
||||
return (_libelf_getphdr(e, ELFCLASS32));
|
||||
}
|
||||
|
||||
Elf64_Phdr *
|
||||
elf64_getphdr(Elf *e)
|
||||
{
|
||||
return (_libelf_getphdr(e, ELFCLASS64));
|
||||
return (_libelf_getphdr(e, ELFCLASS64));
|
||||
}
|
||||
|
||||
GElf_Phdr *
|
||||
gelf_getphdr(Elf *e, int index, GElf_Phdr *d)
|
||||
{
|
||||
int ec;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
Elf32_Phdr *ep32;
|
||||
Elf64_Phdr *ep64;
|
||||
int ec;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
Elf32_Phdr *ep32;
|
||||
Elf64_Phdr *ep64;
|
||||
|
||||
if (d == NULL || e == NULL ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
|
||||
(e->e_kind != ELF_K_ELF) || index < 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (d == NULL || e == NULL ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
|
||||
(e->e_kind != ELF_K_ELF) || index < 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL ||
|
||||
((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL))
|
||||
return (NULL);
|
||||
if (ec == ELFCLASS32) {
|
||||
if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL ||
|
||||
((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL))
|
||||
return (NULL);
|
||||
|
||||
if (index >= eh32->e_phnum) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (index >= eh32->e_phnum) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ep32 += index;
|
||||
ep32 += index;
|
||||
|
||||
d->p_type = ep32->p_type;
|
||||
d->p_offset = ep32->p_offset;
|
||||
d->p_vaddr = (Elf64_Addr) ep32->p_vaddr;
|
||||
d->p_paddr = (Elf64_Addr) ep32->p_paddr;
|
||||
d->p_filesz = (Elf64_Xword) ep32->p_filesz;
|
||||
d->p_memsz = (Elf64_Xword) ep32->p_memsz;
|
||||
d->p_flags = ep32->p_flags;
|
||||
d->p_align = (Elf64_Xword) ep32->p_align;
|
||||
d->p_type = ep32->p_type;
|
||||
d->p_offset = ep32->p_offset;
|
||||
d->p_vaddr = (Elf64_Addr) ep32->p_vaddr;
|
||||
d->p_paddr = (Elf64_Addr) ep32->p_paddr;
|
||||
d->p_filesz = (Elf64_Xword) ep32->p_filesz;
|
||||
d->p_memsz = (Elf64_Xword) ep32->p_memsz;
|
||||
d->p_flags = ep32->p_flags;
|
||||
d->p_align = (Elf64_Xword) ep32->p_align;
|
||||
|
||||
} else {
|
||||
if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL ||
|
||||
(ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL)
|
||||
return (NULL);
|
||||
} else {
|
||||
if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL ||
|
||||
(ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (index >= eh64->e_phnum) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (index >= eh64->e_phnum) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ep64 += index;
|
||||
ep64 += index;
|
||||
|
||||
*d = *ep64;
|
||||
}
|
||||
*d = *ep64;
|
||||
}
|
||||
|
||||
return (d);
|
||||
return (d);
|
||||
}
|
||||
|
||||
Elf32_Phdr *
|
||||
elf32_newphdr(Elf *e, size_t count)
|
||||
{
|
||||
return (_libelf_newphdr(e, ELFCLASS32, count));
|
||||
return (_libelf_newphdr(e, ELFCLASS32, count));
|
||||
}
|
||||
|
||||
Elf64_Phdr *
|
||||
elf64_newphdr(Elf *e, size_t count)
|
||||
{
|
||||
return (_libelf_newphdr(e, ELFCLASS64, count));
|
||||
return (_libelf_newphdr(e, ELFCLASS64, count));
|
||||
}
|
||||
|
||||
void *
|
||||
gelf_newphdr(Elf *e, size_t count)
|
||||
{
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
return (_libelf_newphdr(e, e->e_class, count));
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
return (_libelf_newphdr(e, e->e_class, count));
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s)
|
||||
{
|
||||
int ec, phnum;
|
||||
void *ehdr;
|
||||
Elf32_Phdr *ph32;
|
||||
Elf64_Phdr *ph64;
|
||||
int ec, phnum;
|
||||
void *ehdr;
|
||||
Elf32_Phdr *ph32;
|
||||
Elf64_Phdr *ph64;
|
||||
|
||||
if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (e->e_cmd == ELF_C_READ) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e->e_cmd == ELF_C_READ) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (0);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
|
||||
else
|
||||
phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
|
||||
if (ec == ELFCLASS32)
|
||||
phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
|
||||
else
|
||||
phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
|
||||
|
||||
if (ndx < 0 || ndx > phnum) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ndx < 0 || ndx > phnum) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS64) {
|
||||
ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx;
|
||||
*ph64 = *s;
|
||||
return (1);
|
||||
}
|
||||
(void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx;
|
||||
if (ec == ELFCLASS64) {
|
||||
ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx;
|
||||
*ph64 = *s;
|
||||
return (1);
|
||||
}
|
||||
|
||||
ph32->p_type = s->p_type;
|
||||
ph32->p_flags = s->p_flags;
|
||||
LIBELF_COPY_U32(ph32, s, p_offset);
|
||||
LIBELF_COPY_U32(ph32, s, p_vaddr);
|
||||
LIBELF_COPY_U32(ph32, s, p_paddr);
|
||||
LIBELF_COPY_U32(ph32, s, p_filesz);
|
||||
LIBELF_COPY_U32(ph32, s, p_memsz);
|
||||
LIBELF_COPY_U32(ph32, s, p_align);
|
||||
ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx;
|
||||
|
||||
(void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
|
||||
ph32->p_type = s->p_type;
|
||||
ph32->p_flags = s->p_flags;
|
||||
LIBELF_COPY_U32(ph32, s, p_offset);
|
||||
LIBELF_COPY_U32(ph32, s, p_vaddr);
|
||||
LIBELF_COPY_U32(ph32, s, p_paddr);
|
||||
LIBELF_COPY_U32(ph32, s, p_filesz);
|
||||
LIBELF_COPY_U32(ph32, s, p_memsz);
|
||||
LIBELF_COPY_U32(ph32, s, p_align);
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,118 +24,139 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_rel.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
GElf_Rel *
|
||||
gelf_getrel(Elf_Data *d, int ndx, GElf_Rel *dst)
|
||||
gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Rel *rel32;
|
||||
Elf64_Rel *rel64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Rel *rel32;
|
||||
Elf64_Rel *rel64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(msz > 0);
|
||||
msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
rel32 = (Elf32_Rel *) d->d_buf + ndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
dst->r_offset = (Elf64_Addr) rel32->r_offset;
|
||||
dst->r_info = (Elf64_Xword) rel32->r_info;
|
||||
if (ec == ELFCLASS32) {
|
||||
rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx;
|
||||
|
||||
} else {
|
||||
dst->r_offset = (Elf64_Addr) rel32->r_offset;
|
||||
dst->r_info = ELF64_R_INFO(
|
||||
(Elf64_Xword) ELF32_R_SYM(rel32->r_info),
|
||||
ELF32_R_TYPE(rel32->r_info));
|
||||
|
||||
rel64 = (Elf64_Rel *) d->d_buf + ndx;
|
||||
} else {
|
||||
|
||||
*dst = *rel64;
|
||||
}
|
||||
rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
|
||||
|
||||
return (dst);
|
||||
*dst = *rel64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_rel(Elf_Data *d, int ndx, GElf_Rel *dr)
|
||||
gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Rel *rel32;
|
||||
Elf64_Rel *rel64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Rel *rel32;
|
||||
Elf64_Rel *rel64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || dr == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || dr == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
|
||||
assert(msz > 0);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
rel32 = (Elf32_Rel *) d->d_buf + ndx;
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
LIBELF_COPY_U32(rel32, dr, r_offset);
|
||||
LIBELF_COPY_U32(rel32, dr, r_info);
|
||||
} else {
|
||||
rel64 = (Elf64_Rel *) d->d_buf + ndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
*rel64 = *dr;
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
rel32 = (Elf32_Rel *) d->d_data.d_buf + ndx;
|
||||
|
||||
return (1);
|
||||
LIBELF_COPY_U32(rel32, dr, r_offset);
|
||||
|
||||
if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) ||
|
||||
ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
|
||||
LIBELF_SET_ERROR(RANGE, 0);
|
||||
return (0);
|
||||
}
|
||||
rel32->r_info = ELF32_R_INFO(
|
||||
(Elf32_Word) ELF64_R_SYM(dr->r_info),
|
||||
(Elf32_Word) ELF64_R_TYPE(dr->r_info));
|
||||
} else {
|
||||
rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
|
||||
|
||||
*rel64 = *dr;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,120 +24,142 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_rela.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
GElf_Rela *
|
||||
gelf_getrela(Elf_Data *d, int ndx, GElf_Rela *dst)
|
||||
gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Rela *rela32;
|
||||
Elf64_Rela *rela64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Rela *rela32;
|
||||
Elf64_Rela *rela64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(msz > 0);
|
||||
msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
rela32 = (Elf32_Rela *) d->d_buf + ndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
dst->r_offset = (Elf64_Addr) rela32->r_offset;
|
||||
dst->r_info = (Elf64_Xword) rela32->r_info;
|
||||
dst->r_addend = (Elf64_Sxword) rela32->r_addend;
|
||||
if (ec == ELFCLASS32) {
|
||||
rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx;
|
||||
|
||||
} else {
|
||||
dst->r_offset = (Elf64_Addr) rela32->r_offset;
|
||||
dst->r_info = ELF64_R_INFO(
|
||||
(Elf64_Xword) ELF32_R_SYM(rela32->r_info),
|
||||
ELF32_R_TYPE(rela32->r_info));
|
||||
dst->r_addend = (Elf64_Sxword) rela32->r_addend;
|
||||
|
||||
rela64 = (Elf64_Rela *) d->d_buf + ndx;
|
||||
} else {
|
||||
|
||||
*dst = *rela64;
|
||||
}
|
||||
rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx;
|
||||
|
||||
return (dst);
|
||||
*dst = *rela64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_rela(Elf_Data *d, int ndx, GElf_Rela *dr)
|
||||
gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Rela *rela32;
|
||||
Elf64_Rela *rela64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Rela *rela32;
|
||||
Elf64_Rela *rela64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || dr == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || dr == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
|
||||
assert(msz > 0);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
rela32 = (Elf32_Rela *) d->d_buf + ndx;
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
LIBELF_COPY_U32(rela32, dr, r_offset);
|
||||
LIBELF_COPY_U32(rela32, dr, r_info);
|
||||
LIBELF_COPY_S32(rela32, dr, r_addend);
|
||||
} else {
|
||||
rela64 = (Elf64_Rela *) d->d_buf + ndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
*rela64 = *dr;
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx;
|
||||
|
||||
return (1);
|
||||
LIBELF_COPY_U32(rela32, dr, r_offset);
|
||||
|
||||
if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) ||
|
||||
ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
|
||||
LIBELF_SET_ERROR(RANGE, 0);
|
||||
return (0);
|
||||
}
|
||||
rela32->r_info = ELF32_R_INFO(
|
||||
(Elf32_Word) ELF64_R_SYM(dr->r_info),
|
||||
(Elf32_Word) ELF64_R_TYPE(dr->r_info));
|
||||
|
||||
LIBELF_COPY_S32(rela32, dr, r_addend);
|
||||
} else {
|
||||
rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx;
|
||||
|
||||
*rela64 = *dr;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,105 +24,107 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_shdr.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
Elf32_Shdr *
|
||||
elf32_getshdr(Elf_Scn *s)
|
||||
{
|
||||
return (_libelf_getshdr(s, ELFCLASS32));
|
||||
return (_libelf_getshdr(s, ELFCLASS32));
|
||||
}
|
||||
|
||||
Elf64_Shdr *
|
||||
elf64_getshdr(Elf_Scn *s)
|
||||
{
|
||||
return (_libelf_getshdr(s, ELFCLASS64));
|
||||
return (_libelf_getshdr(s, ELFCLASS64));
|
||||
}
|
||||
|
||||
GElf_Shdr *
|
||||
gelf_getshdr(Elf_Scn *s, GElf_Shdr *d)
|
||||
{
|
||||
int ec;
|
||||
void *sh;
|
||||
Elf32_Shdr *sh32;
|
||||
Elf64_Shdr *sh64;
|
||||
int ec;
|
||||
void *sh;
|
||||
Elf32_Shdr *sh32;
|
||||
Elf64_Shdr *sh64;
|
||||
|
||||
if (d == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (d == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL)
|
||||
return (NULL);
|
||||
if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
ec = s->s_elf->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
ec = s->s_elf->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
sh32 = (Elf32_Shdr *) sh;
|
||||
if (ec == ELFCLASS32) {
|
||||
sh32 = (Elf32_Shdr *) sh;
|
||||
|
||||
d->sh_name = sh32->sh_name;
|
||||
d->sh_type = sh32->sh_type;
|
||||
d->sh_flags = (Elf64_Xword) sh32->sh_flags;
|
||||
d->sh_addr = (Elf64_Addr) sh32->sh_addr;
|
||||
d->sh_offset = (Elf64_Off) sh32->sh_offset;
|
||||
d->sh_size = (Elf64_Xword) sh32->sh_size;
|
||||
d->sh_link = sh32->sh_link;
|
||||
d->sh_info = sh32->sh_info;
|
||||
d->sh_addralign = (Elf64_Xword) sh32->sh_addralign;
|
||||
d->sh_entsize = (Elf64_Xword) sh32->sh_entsize;
|
||||
} else {
|
||||
sh64 = (Elf64_Shdr *) sh;
|
||||
*d = *sh64;
|
||||
}
|
||||
d->sh_name = sh32->sh_name;
|
||||
d->sh_type = sh32->sh_type;
|
||||
d->sh_flags = (Elf64_Xword) sh32->sh_flags;
|
||||
d->sh_addr = (Elf64_Addr) sh32->sh_addr;
|
||||
d->sh_offset = (Elf64_Off) sh32->sh_offset;
|
||||
d->sh_size = (Elf64_Xword) sh32->sh_size;
|
||||
d->sh_link = sh32->sh_link;
|
||||
d->sh_info = sh32->sh_info;
|
||||
d->sh_addralign = (Elf64_Xword) sh32->sh_addralign;
|
||||
d->sh_entsize = (Elf64_Xword) sh32->sh_entsize;
|
||||
} else {
|
||||
sh64 = (Elf64_Shdr *) sh;
|
||||
*d = *sh64;
|
||||
}
|
||||
|
||||
return (d);
|
||||
return (d);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf32_Shdr *sh32;
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf32_Shdr *sh32;
|
||||
|
||||
|
||||
if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL ||
|
||||
e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL ||
|
||||
e->e_kind != ELF_K_ELF ||
|
||||
((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (e->e_cmd == ELF_C_READ) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e->e_cmd == ELF_C_READ) {
|
||||
LIBELF_SET_ERROR(MODE, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS64) {
|
||||
scn->s_shdr.s_shdr64 = *s;
|
||||
return (1);
|
||||
}
|
||||
(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
sh32 = &scn->s_shdr.s_shdr32;
|
||||
if (ec == ELFCLASS64) {
|
||||
scn->s_shdr.s_shdr64 = *s;
|
||||
return (1);
|
||||
}
|
||||
|
||||
sh32->sh_name = s->sh_name;
|
||||
sh32->sh_type = s->sh_type;
|
||||
LIBELF_COPY_U32(sh32, s, sh_flags);
|
||||
LIBELF_COPY_U32(sh32, s, sh_addr);
|
||||
LIBELF_COPY_U32(sh32, s, sh_offset);
|
||||
LIBELF_COPY_U32(sh32, s, sh_size);
|
||||
sh32->sh_link = s->sh_link;
|
||||
sh32->sh_info = s->sh_info;
|
||||
LIBELF_COPY_U32(sh32, s, sh_addralign);
|
||||
LIBELF_COPY_U32(sh32, s, sh_entsize);
|
||||
sh32 = &scn->s_shdr.s_shdr32;
|
||||
|
||||
(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
sh32->sh_name = s->sh_name;
|
||||
sh32->sh_type = s->sh_type;
|
||||
LIBELF_COPY_U32(sh32, s, sh_flags);
|
||||
LIBELF_COPY_U32(sh32, s, sh_addr);
|
||||
LIBELF_COPY_U32(sh32, s, sh_offset);
|
||||
LIBELF_COPY_U32(sh32, s, sh_size);
|
||||
sh32->sh_link = s->sh_link;
|
||||
sh32->sh_info = s->sh_info;
|
||||
LIBELF_COPY_U32(sh32, s, sh_addralign);
|
||||
LIBELF_COPY_U32(sh32, s, sh_entsize);
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,128 +24,136 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_sym.c 3177 2015-03-30 18:19:41Z emaste $");
|
||||
|
||||
GElf_Sym *
|
||||
gelf_getsym(Elf_Data *d, int ndx, GElf_Sym *dst)
|
||||
gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Sym *sym32;
|
||||
Elf64_Sym *sym64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Sym *sym32;
|
||||
Elf64_Sym *sym64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(msz > 0);
|
||||
msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
sym32 = (Elf32_Sym *) d->d_buf + ndx;
|
||||
if (ec == ELFCLASS32) {
|
||||
sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx;
|
||||
|
||||
dst->st_name = sym32->st_name;
|
||||
dst->st_value = (Elf64_Addr) sym32->st_value;
|
||||
dst->st_size = (Elf64_Xword) sym32->st_size;
|
||||
dst->st_info = ELF64_ST_INFO(ELF32_ST_BIND(sym32->st_info),
|
||||
ELF32_ST_TYPE(sym32->st_info));
|
||||
dst->st_other = sym32->st_other;
|
||||
dst->st_shndx = sym32->st_shndx;
|
||||
} else {
|
||||
dst->st_name = sym32->st_name;
|
||||
dst->st_value = (Elf64_Addr) sym32->st_value;
|
||||
dst->st_size = (Elf64_Xword) sym32->st_size;
|
||||
dst->st_info = sym32->st_info;
|
||||
dst->st_other = sym32->st_other;
|
||||
dst->st_shndx = sym32->st_shndx;
|
||||
} else {
|
||||
sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx;
|
||||
|
||||
sym64 = (Elf64_Sym *) d->d_buf + ndx;
|
||||
*dst = *sym64;
|
||||
}
|
||||
|
||||
*dst = *sym64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_sym(Elf_Data *d, int ndx, GElf_Sym *gs)
|
||||
gelf_update_sym(Elf_Data *ed, int ndx, GElf_Sym *gs)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
Elf32_Sym *sym32;
|
||||
Elf64_Sym *sym64;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
Elf32_Sym *sym32;
|
||||
Elf64_Sym *sym64;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if (d == NULL || ndx < 0 || gs == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (d == NULL || ndx < 0 || gs == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
|
||||
assert(msz > 0);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (msz * ndx >= d->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
sym32 = (Elf32_Sym *) d->d_buf + ndx;
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
sym32->st_name = gs->st_name;
|
||||
sym32->st_info = gs->st_info;
|
||||
sym32->st_other = gs->st_other;
|
||||
sym32->st_shndx = gs->st_shndx;
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
LIBELF_COPY_U32(sym32, gs, st_value);
|
||||
LIBELF_COPY_U32(sym32, gs, st_size);
|
||||
} else {
|
||||
sym64 = (Elf64_Sym *) d->d_buf + ndx;
|
||||
if (ec == ELFCLASS32) {
|
||||
sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx;
|
||||
|
||||
*sym64 = *gs;
|
||||
}
|
||||
sym32->st_name = gs->st_name;
|
||||
sym32->st_info = gs->st_info;
|
||||
sym32->st_other = gs->st_other;
|
||||
sym32->st_shndx = gs->st_shndx;
|
||||
|
||||
return (1);
|
||||
LIBELF_COPY_U32(sym32, gs, st_value);
|
||||
LIBELF_COPY_U32(sym32, gs, st_size);
|
||||
} else {
|
||||
sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx;
|
||||
|
||||
*sym64 = *gs;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
152
ext/libelf/gelf_syminfo.c
Normal file
152
ext/libelf/gelf_syminfo.c
Normal file
@@ -0,0 +1,152 @@
|
||||
/*-
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <gelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_syminfo.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
GElf_Syminfo *
|
||||
gelf_getsyminfo(Elf_Data *ed, int ndx, GElf_Syminfo *dst)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
Elf32_Syminfo *syminfo32;
|
||||
Elf64_Syminfo *syminfo64;
|
||||
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (d == NULL || ndx < 0 || dst == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
|
||||
syminfo32 = (Elf32_Syminfo *) d->d_data.d_buf + ndx;
|
||||
|
||||
dst->si_boundto = syminfo32->si_boundto;
|
||||
dst->si_flags = syminfo32->si_flags;
|
||||
|
||||
} else {
|
||||
|
||||
syminfo64 = (Elf64_Syminfo *) d->d_data.d_buf + ndx;
|
||||
|
||||
*dst = *syminfo64;
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_syminfo(Elf_Data *ed, int ndx, GElf_Syminfo *gs)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *d;
|
||||
Elf32_Syminfo *syminfo32;
|
||||
Elf64_Syminfo *syminfo64;
|
||||
|
||||
d = (struct _Libelf_Data *) ed;
|
||||
|
||||
if (d == NULL || ndx < 0 || gs == NULL ||
|
||||
(scn = d->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
if (msz * (size_t) ndx >= d->d_data.d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
syminfo32 = (Elf32_Syminfo *) d->d_data.d_buf + ndx;
|
||||
|
||||
syminfo32->si_boundto = gs->si_boundto;
|
||||
syminfo32->si_flags = gs->si_flags;
|
||||
|
||||
} else {
|
||||
syminfo64 = (Elf64_Syminfo *) d->d_data.d_buf + ndx;
|
||||
|
||||
*syminfo64 = *gs;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,103 +24,114 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_symshndx.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
GElf_Sym *
|
||||
gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst,
|
||||
Elf32_Word *shindex)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *ld, *lid;
|
||||
|
||||
if (gelf_getsym(d, ndx, dst) == 0)
|
||||
return (NULL);
|
||||
ld = (struct _Libelf_Data *) d;
|
||||
lid = (struct _Libelf_Data *) id;
|
||||
|
||||
if (id == NULL || (scn = id->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL || (e != d->d_scn->s_elf) ||
|
||||
shindex == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (gelf_getsym(d, ndx, dst) == 0)
|
||||
return (NULL);
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (lid == NULL || (scn = lid->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf) ||
|
||||
shindex == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
|
||||
id->d_type != ELF_T_WORD) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
|
||||
id->d_type != ELF_T_WORD) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(msz > 0);
|
||||
msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
|
||||
|
||||
if (msz * ndx >= id->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
*shindex = ((Elf32_Word *) id->d_buf)[ndx];
|
||||
if (msz * (size_t) ndx >= id->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (dst);
|
||||
*shindex = ((Elf32_Word *) id->d_buf)[ndx];
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
int
|
||||
gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs,
|
||||
Elf32_Word xindex)
|
||||
{
|
||||
int ec;
|
||||
Elf *e;
|
||||
Elf_Scn *scn;
|
||||
size_t msz;
|
||||
uint32_t sh_type;
|
||||
int ec;
|
||||
Elf *e;
|
||||
size_t msz;
|
||||
Elf_Scn *scn;
|
||||
uint32_t sh_type;
|
||||
struct _Libelf_Data *ld, *lid;
|
||||
|
||||
if (gelf_update_sym(d, ndx, gs) == 0)
|
||||
return (0);
|
||||
ld = (struct _Libelf_Data *) d;
|
||||
lid = (struct _Libelf_Data *) id;
|
||||
|
||||
if (id == NULL || (scn = id->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL || (e != d->d_scn->s_elf)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (gelf_update_sym(d, ndx, gs) == 0)
|
||||
return (0);
|
||||
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
if (lid == NULL || (scn = lid->d_scn) == NULL ||
|
||||
(e = scn->s_elf) == NULL || (e != ld->d_scn->s_elf)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
ec = e->e_class;
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
|
||||
d->d_type != ELF_T_WORD) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
if (ec == ELFCLASS32)
|
||||
sh_type = scn->s_shdr.s_shdr32.sh_type;
|
||||
else
|
||||
sh_type = scn->s_shdr.s_shdr64.sh_type;
|
||||
|
||||
msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
|
||||
assert(msz > 0);
|
||||
if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
|
||||
d->d_type != ELF_T_WORD) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (msz * ndx >= id->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
|
||||
|
||||
*(((Elf32_Word *) id->d_buf) + ndx) = xindex;
|
||||
assert(msz > 0);
|
||||
assert(ndx >= 0);
|
||||
|
||||
return (1);
|
||||
if (msz * (size_t) ndx >= id->d_size) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
*(((Elf32_Word *) id->d_buf) + ndx) = xindex;
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,53 +24,56 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: gelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
Elf_Data *
|
||||
elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
|
||||
{
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOFILE);
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOFILE);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
|
||||
{
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOFILE);
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOFILE);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
|
||||
{
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOMEMORY);
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOMEMORY);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
|
||||
{
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOMEMORY);
|
||||
return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOMEMORY);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
gelf_xlatetom(Elf *e, Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
|
||||
gelf_xlatetom(Elf *e, Elf_Data *dst, const Elf_Data *src,
|
||||
unsigned int encoding)
|
||||
{
|
||||
if (e != NULL)
|
||||
return (_libelf_xlate(dst, src, encoding, e->e_class,
|
||||
ELF_TOMEMORY));
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
if (e != NULL)
|
||||
return (_libelf_xlate(dst, src, encoding, e->e_class,
|
||||
ELF_TOMEMORY));
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
gelf_xlatetof(Elf *e, Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
|
||||
gelf_xlatetof(Elf *e, Elf_Data *dst, const Elf_Data *src,
|
||||
unsigned int encoding)
|
||||
{
|
||||
if (e != NULL)
|
||||
return (_libelf_xlate(dst, src, encoding, e->e_class,
|
||||
ELF_TOFILE));
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
if (e != NULL)
|
||||
return (_libelf_xlate(dst, src, encoding, e->e_class,
|
||||
ELF_TOFILE));
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -23,7 +23,7 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/libelf.h,v 1.1 2006/11/11 17:16:33 jkoshy Exp $
|
||||
* $Id: libelf.h 3174 2015-03-27 17:13:41Z emaste $
|
||||
*/
|
||||
|
||||
#ifndef _LIBELF_H_
|
||||
@@ -31,13 +31,7 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "elf_queue.h"
|
||||
#include "elf32.h"
|
||||
#include "elf64.h"
|
||||
#include <elfdefinitions.h>
|
||||
|
||||
/* Library private data structures */
|
||||
typedef struct _Elf Elf;
|
||||
@@ -45,11 +39,11 @@ typedef struct _Elf_Scn Elf_Scn;
|
||||
|
||||
/* File types */
|
||||
typedef enum {
|
||||
ELF_K_NONE = 0,
|
||||
ELF_K_AR, /* `ar' archives */
|
||||
ELF_K_COFF, /* COFF files (unsupported) */
|
||||
ELF_K_ELF, /* ELF files */
|
||||
ELF_K_NUM
|
||||
ELF_K_NONE = 0,
|
||||
ELF_K_AR, /* `ar' archives */
|
||||
ELF_K_COFF, /* COFF files (unsupported) */
|
||||
ELF_K_ELF, /* ELF files */
|
||||
ELF_K_NUM
|
||||
} Elf_Kind;
|
||||
|
||||
#define ELF_K_FIRST ELF_K_NONE
|
||||
@@ -57,46 +51,47 @@ typedef enum {
|
||||
|
||||
/* Data types */
|
||||
typedef enum {
|
||||
ELF_T_ADDR,
|
||||
ELF_T_BYTE,
|
||||
ELF_T_CAP,
|
||||
ELF_T_DYN,
|
||||
ELF_T_EHDR,
|
||||
ELF_T_HALF,
|
||||
ELF_T_LWORD,
|
||||
ELF_T_MOVE,
|
||||
ELF_T_MOVEP,
|
||||
ELF_T_NOTE,
|
||||
ELF_T_OFF,
|
||||
ELF_T_PHDR,
|
||||
ELF_T_REL,
|
||||
ELF_T_RELA,
|
||||
ELF_T_SHDR,
|
||||
ELF_T_SWORD,
|
||||
ELF_T_SXWORD,
|
||||
ELF_T_SYMINFO,
|
||||
ELF_T_SYM,
|
||||
ELF_T_VDEF,
|
||||
ELF_T_VNEED,
|
||||
ELF_T_WORD,
|
||||
ELF_T_XWORD,
|
||||
ELF_T_NUM
|
||||
ELF_T_ADDR,
|
||||
ELF_T_BYTE,
|
||||
ELF_T_CAP,
|
||||
ELF_T_DYN,
|
||||
ELF_T_EHDR,
|
||||
ELF_T_HALF,
|
||||
ELF_T_LWORD,
|
||||
ELF_T_MOVE,
|
||||
ELF_T_MOVEP,
|
||||
ELF_T_NOTE,
|
||||
ELF_T_OFF,
|
||||
ELF_T_PHDR,
|
||||
ELF_T_REL,
|
||||
ELF_T_RELA,
|
||||
ELF_T_SHDR,
|
||||
ELF_T_SWORD,
|
||||
ELF_T_SXWORD,
|
||||
ELF_T_SYMINFO,
|
||||
ELF_T_SYM,
|
||||
ELF_T_VDEF,
|
||||
ELF_T_VNEED,
|
||||
ELF_T_WORD,
|
||||
ELF_T_XWORD,
|
||||
ELF_T_GNUHASH, /* GNU style hash tables. */
|
||||
ELF_T_NUM
|
||||
} Elf_Type;
|
||||
|
||||
#define ELF_T_FIRST ELF_T_ADDR
|
||||
#define ELF_T_LAST ELF_T_XWORD
|
||||
#define ELF_T_LAST ELF_T_GNUHASH
|
||||
|
||||
/* Commands */
|
||||
typedef enum {
|
||||
ELF_C_NULL = 0,
|
||||
ELF_C_CLR,
|
||||
ELF_C_FDDONE,
|
||||
ELF_C_FDREAD,
|
||||
ELF_C_RDWR,
|
||||
ELF_C_READ,
|
||||
ELF_C_SET,
|
||||
ELF_C_WRITE,
|
||||
ELF_C_NUM
|
||||
ELF_C_NULL = 0,
|
||||
ELF_C_CLR,
|
||||
ELF_C_FDDONE,
|
||||
ELF_C_FDREAD,
|
||||
ELF_C_RDWR,
|
||||
ELF_C_READ,
|
||||
ELF_C_SET,
|
||||
ELF_C_WRITE,
|
||||
ELF_C_NUM
|
||||
} Elf_Cmd;
|
||||
|
||||
#define ELF_C_FIRST ELF_C_NULL
|
||||
@@ -107,22 +102,15 @@ typedef enum {
|
||||
* ELF section.
|
||||
*/
|
||||
typedef struct _Elf_Data {
|
||||
/*
|
||||
* `Public' members that are part of the ELF(3) API.
|
||||
*/
|
||||
uint64_t d_align;
|
||||
void *d_buf;
|
||||
uint64_t d_off;
|
||||
uint64_t d_size;
|
||||
Elf_Type d_type;
|
||||
unsigned int d_version;
|
||||
|
||||
/*
|
||||
* Members that are not part of the public API.
|
||||
*/
|
||||
Elf_Scn *d_scn; /* containing section */
|
||||
unsigned int d_flags;
|
||||
STAILQ_ENTRY(_Elf_Data) d_next;
|
||||
/*
|
||||
* `Public' members that are part of the ELF(3) API.
|
||||
*/
|
||||
uint64_t d_align;
|
||||
void *d_buf;
|
||||
uint64_t d_off;
|
||||
uint64_t d_size;
|
||||
Elf_Type d_type;
|
||||
unsigned int d_version;
|
||||
} Elf_Data;
|
||||
|
||||
/*
|
||||
@@ -130,13 +118,18 @@ typedef struct _Elf_Data {
|
||||
* header.
|
||||
*/
|
||||
typedef struct {
|
||||
time_t ar_date;
|
||||
char *ar_name; /* archive member name */
|
||||
gid_t ar_gid;
|
||||
mode_t ar_mode;
|
||||
char *ar_rawname; /* 'raw' member name */
|
||||
size_t ar_size;
|
||||
uid_t ar_uid;
|
||||
time_t ar_date;
|
||||
char *ar_name; /* archive member name */
|
||||
gid_t ar_gid;
|
||||
mode_t ar_mode;
|
||||
char *ar_rawname; /* 'raw' member name */
|
||||
size_t ar_size;
|
||||
uid_t ar_uid;
|
||||
|
||||
/*
|
||||
* Members that are not part of the public API.
|
||||
*/
|
||||
unsigned int ar_flags;
|
||||
} Elf_Arhdr;
|
||||
|
||||
/*
|
||||
@@ -144,9 +137,9 @@ typedef struct {
|
||||
* symbol table.
|
||||
*/
|
||||
typedef struct {
|
||||
off_t as_off; /* byte offset to member's header */
|
||||
unsigned long as_hash; /* elf_hash() value for name */
|
||||
char *as_name; /* null terminated symbol name */
|
||||
off_t as_off; /* byte offset to member's header */
|
||||
unsigned long as_hash; /* elf_hash() value for name */
|
||||
char *as_name; /* null terminated symbol name */
|
||||
} Elf_Arsym;
|
||||
|
||||
/*
|
||||
@@ -154,22 +147,22 @@ typedef struct {
|
||||
*/
|
||||
|
||||
enum Elf_Error {
|
||||
ELF_E_NONE, /* No error */
|
||||
ELF_E_ARCHIVE, /* Malformed ar(1) archive */
|
||||
ELF_E_ARGUMENT, /* Invalid argument */
|
||||
ELF_E_CLASS, /* Mismatched ELF class */
|
||||
ELF_E_DATA, /* Invalid data descriptor */
|
||||
ELF_E_HEADER, /* Missing or malformed ELF header */
|
||||
ELF_E_IO, /* I/O error */
|
||||
ELF_E_LAYOUT, /* Layout constraint violation */
|
||||
ELF_E_MODE, /* Wrong mode for ELF descriptor */
|
||||
ELF_E_RANGE, /* Value out of range */
|
||||
ELF_E_RESOURCE, /* Resource exhaustion */
|
||||
ELF_E_SECTION, /* Invalid section descriptor */
|
||||
ELF_E_SEQUENCE, /* API calls out of sequence */
|
||||
ELF_E_UNIMPL, /* Feature is unimplemented */
|
||||
ELF_E_VERSION, /* Unknown API version */
|
||||
ELF_E_NUM /* Max error number */
|
||||
ELF_E_NONE, /* No error */
|
||||
ELF_E_ARCHIVE, /* Malformed ar(1) archive */
|
||||
ELF_E_ARGUMENT, /* Invalid argument */
|
||||
ELF_E_CLASS, /* Mismatched ELF class */
|
||||
ELF_E_DATA, /* Invalid data descriptor */
|
||||
ELF_E_HEADER, /* Missing or malformed ELF header */
|
||||
ELF_E_IO, /* I/O error */
|
||||
ELF_E_LAYOUT, /* Layout constraint violation */
|
||||
ELF_E_MODE, /* Wrong mode for ELF descriptor */
|
||||
ELF_E_RANGE, /* Value out of range */
|
||||
ELF_E_RESOURCE, /* Resource exhaustion */
|
||||
ELF_E_SECTION, /* Invalid section descriptor */
|
||||
ELF_E_SEQUENCE, /* API calls out of sequence */
|
||||
ELF_E_UNIMPL, /* Feature is unimplemented */
|
||||
ELF_E_VERSION, /* Unknown API version */
|
||||
ELF_E_NUM /* Max error number */
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -179,13 +172,23 @@ enum Elf_Error {
|
||||
#define ELF_F_LAYOUT 0x001U /* application will layout the file */
|
||||
#define ELF_F_DIRTY 0x002U /* a section or ELF file is dirty */
|
||||
|
||||
/* ELF(3) API extensions. */
|
||||
#define ELF_F_ARCHIVE 0x100U /* archive creation */
|
||||
#define ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
Elf *elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
|
||||
int elf_cntl(Elf *_elf, Elf_Cmd _cmd);
|
||||
int elf_end(Elf *_elf);
|
||||
const char *elf_errmsg(int _error);
|
||||
int elf_errno(void);
|
||||
void elf_fill(int _fill);
|
||||
unsigned int elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, unsigned int _flags);
|
||||
unsigned int elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
|
||||
unsigned int _flags);
|
||||
unsigned int elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd,
|
||||
unsigned int _flags);
|
||||
unsigned int elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
|
||||
unsigned int elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
|
||||
unsigned int elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
|
||||
@@ -196,10 +199,13 @@ Elf_Arsym *elf_getarsym(Elf *_elf, size_t *_ptr);
|
||||
off_t elf_getbase(Elf *_elf);
|
||||
Elf_Data *elf_getdata(Elf_Scn *, Elf_Data *);
|
||||
char *elf_getident(Elf *_elf, size_t *_ptr);
|
||||
int elf_getphnum(Elf *_elf, size_t *_dst);
|
||||
int elf_getphdrnum(Elf *_elf, size_t *_dst);
|
||||
int elf_getphnum(Elf *_elf, size_t *_dst); /* Deprecated */
|
||||
Elf_Scn *elf_getscn(Elf *_elf, size_t _index);
|
||||
int elf_getshnum(Elf *_elf, size_t *_dst);
|
||||
int elf_getshstrndx(Elf *_elf, size_t *_dst);
|
||||
int elf_getshdrnum(Elf *_elf, size_t *_dst);
|
||||
int elf_getshnum(Elf *_elf, size_t *_dst); /* Deprecated */
|
||||
int elf_getshdrstrndx(Elf *_elf, size_t *_dst);
|
||||
int elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */
|
||||
unsigned long elf_hash(const char *_name);
|
||||
Elf_Kind elf_kind(Elf *_elf);
|
||||
Elf *elf_memory(char *_image, size_t _size);
|
||||
@@ -208,6 +214,8 @@ Elf_Data *elf_newdata(Elf_Scn *_scn);
|
||||
Elf_Scn *elf_newscn(Elf *_elf);
|
||||
Elf_Scn *elf_nextscn(Elf *_elf, Elf_Scn *_scn);
|
||||
Elf_Cmd elf_next(Elf *_elf);
|
||||
Elf *elf_open(int _fd);
|
||||
Elf *elf_openmemory(char *_image, size_t _size);
|
||||
off_t elf_rand(Elf *_elf, off_t _off);
|
||||
Elf_Data *elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
|
||||
char *elf_rawfile(Elf *_elf, size_t *_size);
|
||||
@@ -218,30 +226,29 @@ unsigned int elf_version(unsigned int _version);
|
||||
|
||||
long elf32_checksum(Elf *_elf);
|
||||
size_t elf32_fsize(Elf_Type _type, size_t _count,
|
||||
unsigned int _version);
|
||||
unsigned int _version);
|
||||
Elf32_Ehdr *elf32_getehdr(Elf *_elf);
|
||||
Elf32_Phdr *elf32_getphdr(Elf *_elf);
|
||||
Elf32_Shdr *elf32_getshdr(Elf_Scn *_scn);
|
||||
Elf32_Ehdr *elf32_newehdr(Elf *_elf);
|
||||
Elf32_Phdr *elf32_newphdr(Elf *_elf, size_t _count);
|
||||
Elf_Data *elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
|
||||
unsigned int _enc);
|
||||
unsigned int _enc);
|
||||
Elf_Data *elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
|
||||
unsigned int _enc);
|
||||
unsigned int _enc);
|
||||
|
||||
long elf64_checksum(Elf *_elf);
|
||||
size_t elf64_fsize(Elf_Type _type, size_t _count,
|
||||
unsigned int _version);
|
||||
unsigned int _version);
|
||||
Elf64_Ehdr *elf64_getehdr(Elf *_elf);
|
||||
Elf64_Phdr *elf64_getphdr(Elf *_elf);
|
||||
Elf64_Shdr *elf64_getshdr(Elf_Scn *_scn);
|
||||
Elf64_Ehdr *elf64_newehdr(Elf *_elf);
|
||||
Elf64_Phdr *elf64_newphdr(Elf *_elf, size_t _count);
|
||||
Elf_Data *elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
|
||||
unsigned int _enc);
|
||||
unsigned int _enc);
|
||||
Elf_Data *elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
|
||||
unsigned int _enc);
|
||||
|
||||
unsigned int _enc);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -26,105 +26,110 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
//#include <machine/elf.h>
|
||||
//#include <machine/endian.h>
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_align.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
struct align {
|
||||
int a32;
|
||||
int a64;
|
||||
unsigned int a32;
|
||||
unsigned int a64;
|
||||
};
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define MALIGN(N) { \
|
||||
.a32 = __alignof__(Elf32_##N), \
|
||||
.a64 = __alignof__(Elf64_##N) \
|
||||
}
|
||||
.a32 = __alignof__(Elf32_##N), \
|
||||
.a64 = __alignof__(Elf64_##N) \
|
||||
}
|
||||
#define MALIGN64(V) { \
|
||||
.a32 = 0, \
|
||||
.a64 = __alignof__(Elf64_##V) \
|
||||
}
|
||||
.a32 = 0, \
|
||||
.a64 = __alignof__(Elf64_##V) \
|
||||
}
|
||||
#define MALIGN_WORD() { \
|
||||
.a32 = __alignof__(int32_t), \
|
||||
.a64 = __alignof__(int64_t) \
|
||||
}
|
||||
#else
|
||||
#error Need the __alignof__ builtin.
|
||||
#endif
|
||||
#define UNSUPPORTED() { \
|
||||
.a32 = 0, \
|
||||
.a64 = 0 \
|
||||
}
|
||||
.a32 = 0, \
|
||||
.a64 = 0 \
|
||||
}
|
||||
|
||||
static struct align malign[ELF_T_NUM] = {
|
||||
[ELF_T_ADDR] = MALIGN(Addr),
|
||||
[ELF_T_BYTE] = { .a32 = 1, .a64 = 1 },
|
||||
[ELF_T_CAP] = MALIGN(Cap),
|
||||
[ELF_T_DYN] = MALIGN(Dyn),
|
||||
[ELF_T_EHDR] = MALIGN(Ehdr),
|
||||
[ELF_T_HALF] = MALIGN(Half),
|
||||
[ELF_T_LWORD] = MALIGN(Lword),
|
||||
[ELF_T_MOVE] = MALIGN(Move),
|
||||
[ELF_T_MOVEP] = UNSUPPORTED(),
|
||||
[ELF_T_NOTE] = MALIGN(Nhdr),
|
||||
[ELF_T_OFF] = MALIGN(Off),
|
||||
[ELF_T_PHDR] = MALIGN(Phdr),
|
||||
[ELF_T_REL] = MALIGN(Rel),
|
||||
[ELF_T_RELA] = MALIGN(Rela),
|
||||
[ELF_T_SHDR] = MALIGN(Shdr),
|
||||
[ELF_T_SWORD] = MALIGN(Sword),
|
||||
[ELF_T_SXWORD] = MALIGN64(Sxword),
|
||||
[ELF_T_SYM] = MALIGN(Sym),
|
||||
[ELF_T_SYMINFO] = MALIGN(Syminfo),
|
||||
[ELF_T_VDEF] = MALIGN(Verdef),
|
||||
[ELF_T_VNEED] = MALIGN(Verneed),
|
||||
[ELF_T_WORD] = MALIGN(Word),
|
||||
[ELF_T_XWORD] = MALIGN64(Xword)
|
||||
[ELF_T_ADDR] = MALIGN(Addr),
|
||||
[ELF_T_BYTE] = { .a32 = 1, .a64 = 1 },
|
||||
[ELF_T_CAP] = MALIGN(Cap),
|
||||
[ELF_T_DYN] = MALIGN(Dyn),
|
||||
[ELF_T_EHDR] = MALIGN(Ehdr),
|
||||
[ELF_T_HALF] = MALIGN(Half),
|
||||
[ELF_T_LWORD] = MALIGN(Lword),
|
||||
[ELF_T_MOVE] = MALIGN(Move),
|
||||
[ELF_T_MOVEP] = UNSUPPORTED(),
|
||||
[ELF_T_NOTE] = MALIGN(Nhdr),
|
||||
[ELF_T_OFF] = MALIGN(Off),
|
||||
[ELF_T_PHDR] = MALIGN(Phdr),
|
||||
[ELF_T_REL] = MALIGN(Rel),
|
||||
[ELF_T_RELA] = MALIGN(Rela),
|
||||
[ELF_T_SHDR] = MALIGN(Shdr),
|
||||
[ELF_T_SWORD] = MALIGN(Sword),
|
||||
[ELF_T_SXWORD] = MALIGN64(Sxword),
|
||||
[ELF_T_SYM] = MALIGN(Sym),
|
||||
[ELF_T_SYMINFO] = MALIGN(Syminfo),
|
||||
[ELF_T_VDEF] = MALIGN(Verdef),
|
||||
[ELF_T_VNEED] = MALIGN(Verneed),
|
||||
[ELF_T_WORD] = MALIGN(Word),
|
||||
[ELF_T_XWORD] = MALIGN64(Xword),
|
||||
[ELF_T_GNUHASH] = MALIGN_WORD()
|
||||
};
|
||||
|
||||
int
|
||||
unsigned int
|
||||
_libelf_malign(Elf_Type t, int elfclass)
|
||||
{
|
||||
if (t >= ELF_T_NUM || (int) t < 0)
|
||||
return (0);
|
||||
if (t >= ELF_T_NUM || (int) t < 0)
|
||||
return (0);
|
||||
|
||||
return (elfclass == ELFCLASS32 ? malign[t].a32 :
|
||||
malign[t].a64);
|
||||
return (elfclass == ELFCLASS32 ? malign[t].a32 :
|
||||
malign[t].a64);
|
||||
}
|
||||
|
||||
#define FALIGN(A32,A64) { .a32 = (A32), .a64 = (A64) }
|
||||
|
||||
static struct align falign[ELF_T_NUM] = {
|
||||
[ELF_T_ADDR] = FALIGN(4,8),
|
||||
[ELF_T_BYTE] = FALIGN(1,1),
|
||||
[ELF_T_CAP] = FALIGN(4,8),
|
||||
[ELF_T_DYN] = FALIGN(4,8),
|
||||
[ELF_T_EHDR] = FALIGN(4,8),
|
||||
[ELF_T_HALF] = FALIGN(2,2),
|
||||
[ELF_T_LWORD] = FALIGN(8,8),
|
||||
[ELF_T_MOVE] = FALIGN(8,8),
|
||||
[ELF_T_MOVEP] = UNSUPPORTED(),
|
||||
[ELF_T_NOTE] = FALIGN(4,4),
|
||||
[ELF_T_OFF] = FALIGN(4,8),
|
||||
[ELF_T_PHDR] = FALIGN(4,8),
|
||||
[ELF_T_REL] = FALIGN(4,8),
|
||||
[ELF_T_RELA] = FALIGN(4,8),
|
||||
[ELF_T_SHDR] = FALIGN(4,8),
|
||||
[ELF_T_SWORD] = FALIGN(4,4),
|
||||
[ELF_T_SXWORD] = FALIGN(0,8),
|
||||
[ELF_T_SYM] = FALIGN(4,8),
|
||||
[ELF_T_SYMINFO] = FALIGN(2,2),
|
||||
[ELF_T_VDEF] = FALIGN(4,4),
|
||||
[ELF_T_VNEED] = FALIGN(4,4),
|
||||
[ELF_T_WORD] = FALIGN(4,4),
|
||||
[ELF_T_XWORD] = FALIGN(0,8)
|
||||
[ELF_T_ADDR] = FALIGN(4,8),
|
||||
[ELF_T_BYTE] = FALIGN(1,1),
|
||||
[ELF_T_CAP] = FALIGN(4,8),
|
||||
[ELF_T_DYN] = FALIGN(4,8),
|
||||
[ELF_T_EHDR] = FALIGN(4,8),
|
||||
[ELF_T_HALF] = FALIGN(2,2),
|
||||
[ELF_T_LWORD] = FALIGN(8,8),
|
||||
[ELF_T_MOVE] = FALIGN(8,8),
|
||||
[ELF_T_MOVEP] = UNSUPPORTED(),
|
||||
[ELF_T_NOTE] = FALIGN(4,4),
|
||||
[ELF_T_OFF] = FALIGN(4,8),
|
||||
[ELF_T_PHDR] = FALIGN(4,8),
|
||||
[ELF_T_REL] = FALIGN(4,8),
|
||||
[ELF_T_RELA] = FALIGN(4,8),
|
||||
[ELF_T_SHDR] = FALIGN(4,8),
|
||||
[ELF_T_SWORD] = FALIGN(4,4),
|
||||
[ELF_T_SXWORD] = FALIGN(0,8),
|
||||
[ELF_T_SYM] = FALIGN(4,8),
|
||||
[ELF_T_SYMINFO] = FALIGN(2,2),
|
||||
[ELF_T_VDEF] = FALIGN(4,4),
|
||||
[ELF_T_VNEED] = FALIGN(4,4),
|
||||
[ELF_T_WORD] = FALIGN(4,4),
|
||||
[ELF_T_XWORD] = FALIGN(0,8),
|
||||
[ELF_T_GNUHASH] = FALIGN(4,8)
|
||||
};
|
||||
|
||||
int
|
||||
unsigned int
|
||||
_libelf_falign(Elf_Type t, int elfclass)
|
||||
{
|
||||
if (t >= ELF_T_NUM || (int) t < 0)
|
||||
return (0);
|
||||
if (t >= ELF_T_NUM || (int) t < 0)
|
||||
return (0);
|
||||
|
||||
return (elfclass == ELFCLASS32 ? falign[t].a32 :
|
||||
falign[t].a64);
|
||||
return (elfclass == ELFCLASS32 ? falign[t].a32 :
|
||||
falign[t].a64);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008,2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -28,179 +28,183 @@
|
||||
* Internal APIs
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "libelf.h"
|
||||
#include <errno.h>
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_allocate.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
Elf *
|
||||
_libelf_allocate_elf(void)
|
||||
{
|
||||
Elf *e;
|
||||
Elf *e;
|
||||
|
||||
if ((e = malloc(sizeof(*e))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, errno);
|
||||
return NULL;
|
||||
}
|
||||
if ((e = malloc(sizeof(*e))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
e->e_activations = 1;
|
||||
e->e_arhdr = NULL;
|
||||
e->e_byteorder = ELFDATANONE;
|
||||
e->e_class = ELFCLASSNONE;
|
||||
e->e_cmd = ELF_C_NULL;
|
||||
e->e_fd = -1;
|
||||
e->e_flags = 0;
|
||||
e->e_kind = ELF_K_NONE;
|
||||
e->e_parent = NULL;
|
||||
e->e_rawfile = NULL;
|
||||
e->e_rawsize = 0;
|
||||
e->e_version = LIBELF_PRIVATE(version);
|
||||
e->e_activations = 1;
|
||||
e->e_hdr.e_rawhdr = NULL;
|
||||
e->e_byteorder = ELFDATANONE;
|
||||
e->e_class = ELFCLASSNONE;
|
||||
e->e_cmd = ELF_C_NULL;
|
||||
e->e_fd = -1;
|
||||
e->e_flags = 0;
|
||||
e->e_kind = ELF_K_NONE;
|
||||
e->e_parent = NULL;
|
||||
e->e_rawfile = NULL;
|
||||
e->e_rawsize = 0;
|
||||
e->e_version = LIBELF_PRIVATE(version);
|
||||
|
||||
(void) memset(&e->e_u, 0, sizeof(e->e_u));
|
||||
(void) memset(&e->e_u, 0, sizeof(e->e_u));
|
||||
|
||||
return (e);
|
||||
return (e);
|
||||
}
|
||||
|
||||
void
|
||||
_libelf_init_elf(Elf *e, Elf_Kind kind)
|
||||
{
|
||||
assert(e != NULL);
|
||||
assert(e->e_kind == ELF_K_NONE);
|
||||
assert(e != NULL);
|
||||
assert(e->e_kind == ELF_K_NONE);
|
||||
|
||||
e->e_kind = kind;
|
||||
e->e_kind = kind;
|
||||
|
||||
switch (kind) {
|
||||
case ELF_K_ELF:
|
||||
STAILQ_INIT(&e->e_u.e_elf.e_scn);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (kind) {
|
||||
case ELF_K_ELF:
|
||||
STAILQ_INIT(&e->e_u.e_elf.e_scn);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#define FREE(P) do { \
|
||||
if (P) \
|
||||
free(P); \
|
||||
} while (0)
|
||||
if (P) \
|
||||
free(P); \
|
||||
} while (0)
|
||||
|
||||
|
||||
Elf *
|
||||
_libelf_release_elf(Elf *e)
|
||||
{
|
||||
switch (e->e_kind) {
|
||||
case ELF_K_AR:
|
||||
FREE(e->e_u.e_ar.e_symtab);
|
||||
break;
|
||||
Elf_Arhdr *arh;
|
||||
|
||||
case ELF_K_ELF:
|
||||
switch (e->e_class) {
|
||||
case ELFCLASS32:
|
||||
FREE(e->e_u.e_elf.e_ehdr.e_ehdr32);
|
||||
FREE(e->e_u.e_elf.e_phdr.e_phdr32);
|
||||
break;
|
||||
case ELFCLASS64:
|
||||
FREE(e->e_u.e_elf.e_ehdr.e_ehdr64);
|
||||
FREE(e->e_u.e_elf.e_phdr.e_phdr64);
|
||||
break;
|
||||
}
|
||||
switch (e->e_kind) {
|
||||
case ELF_K_AR:
|
||||
FREE(e->e_u.e_ar.e_symtab);
|
||||
break;
|
||||
|
||||
assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
|
||||
case ELF_K_ELF:
|
||||
switch (e->e_class) {
|
||||
case ELFCLASS32:
|
||||
FREE(e->e_u.e_elf.e_ehdr.e_ehdr32);
|
||||
FREE(e->e_u.e_elf.e_phdr.e_phdr32);
|
||||
break;
|
||||
case ELFCLASS64:
|
||||
FREE(e->e_u.e_elf.e_ehdr.e_ehdr64);
|
||||
FREE(e->e_u.e_elf.e_phdr.e_phdr64);
|
||||
break;
|
||||
}
|
||||
|
||||
if (e->e_arhdr) {
|
||||
FREE(e->e_arhdr->ar_name);
|
||||
FREE(e->e_arhdr->ar_rawname);
|
||||
free(e->e_arhdr);
|
||||
}
|
||||
assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
|
||||
|
||||
break;
|
||||
if (e->e_flags & LIBELF_F_AR_HEADER) {
|
||||
arh = e->e_hdr.e_arhdr;
|
||||
FREE(arh->ar_name);
|
||||
FREE(arh->ar_rawname);
|
||||
free(arh);
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
free(e);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return (NULL);
|
||||
free(e);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
struct _Libelf_Data *
|
||||
_libelf_allocate_data(Elf_Scn *s)
|
||||
{
|
||||
Elf_Data *d;
|
||||
struct _Libelf_Data *d;
|
||||
|
||||
if ((d = calloc((size_t) 1, sizeof(Elf_Data))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((d = calloc((size_t) 1, sizeof(*d))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
d->d_scn = s;
|
||||
d->d_scn = s;
|
||||
|
||||
return (d);
|
||||
return (d);
|
||||
}
|
||||
|
||||
Elf_Data *
|
||||
_libelf_release_data(Elf_Data *d)
|
||||
struct _Libelf_Data *
|
||||
_libelf_release_data(struct _Libelf_Data *d)
|
||||
{
|
||||
|
||||
if (d->d_flags & LIBELF_F_MALLOCED)
|
||||
free(d->d_buf);
|
||||
if (d->d_flags & LIBELF_F_DATA_MALLOCED)
|
||||
free(d->d_data.d_buf);
|
||||
|
||||
free(d);
|
||||
free(d);
|
||||
|
||||
return (NULL);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
Elf_Scn *
|
||||
_libelf_allocate_scn(Elf *e, size_t ndx)
|
||||
{
|
||||
Elf_Scn *s;
|
||||
Elf_Scn *s;
|
||||
|
||||
if ((s = calloc((size_t) 1, sizeof(Elf_Scn))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, errno);
|
||||
return (NULL);
|
||||
}
|
||||
if ((s = calloc((size_t) 1, sizeof(Elf_Scn))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, errno);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
s->s_elf = e;
|
||||
s->s_ndx = ndx;
|
||||
s->s_elf = e;
|
||||
s->s_ndx = ndx;
|
||||
|
||||
STAILQ_INIT(&s->s_data);
|
||||
STAILQ_INIT(&s->s_rawdata);
|
||||
STAILQ_INIT(&s->s_data);
|
||||
STAILQ_INIT(&s->s_rawdata);
|
||||
|
||||
STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next);
|
||||
STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next);
|
||||
|
||||
return (s);
|
||||
return (s);
|
||||
}
|
||||
|
||||
Elf_Scn *
|
||||
_libelf_release_scn(Elf_Scn *s)
|
||||
{
|
||||
Elf *e;
|
||||
Elf_Data *d, *td;
|
||||
Elf *e;
|
||||
struct _Libelf_Data *d, *td;
|
||||
|
||||
assert(s != NULL);
|
||||
assert(s != NULL);
|
||||
|
||||
STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
|
||||
STAILQ_REMOVE(&s->s_data, d, _Elf_Data, d_next);
|
||||
d = _libelf_release_data(d);
|
||||
}
|
||||
STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
|
||||
STAILQ_REMOVE(&s->s_data, d, _Libelf_Data, d_next);
|
||||
d = _libelf_release_data(d);
|
||||
}
|
||||
|
||||
STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
|
||||
assert((d->d_flags & LIBELF_F_MALLOCED) == 0);
|
||||
STAILQ_REMOVE(&s->s_rawdata, d, _Elf_Data, d_next);
|
||||
d = _libelf_release_data(d);
|
||||
}
|
||||
STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
|
||||
assert((d->d_flags & LIBELF_F_DATA_MALLOCED) == 0);
|
||||
STAILQ_REMOVE(&s->s_rawdata, d, _Libelf_Data, d_next);
|
||||
d = _libelf_release_data(d);
|
||||
}
|
||||
|
||||
e = s->s_elf;
|
||||
e = s->s_elf;
|
||||
|
||||
assert(e != NULL);
|
||||
assert(e != NULL);
|
||||
|
||||
STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next);
|
||||
STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next);
|
||||
|
||||
free(s);
|
||||
free(s);
|
||||
|
||||
return (NULL);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008,2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,14 +24,16 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ar.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
#include "_libelf_ar.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_ar.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
#define LIBELF_NALLOC_SIZE 16
|
||||
|
||||
@@ -46,436 +48,419 @@
|
||||
*
|
||||
* Special considerations for `ar' archives:
|
||||
*
|
||||
* The `ar' header only has space for a 16 character file name. File
|
||||
* names are terminated with a '/', so this effectively leaves 15
|
||||
* characters for the actual file name. In order to accomodate longer
|
||||
* file names, names may be stored in a separate 'string table' and
|
||||
* referenced indirectly by a member header. The string table itself
|
||||
* appears as an archive member with name "// ". An indirect file name
|
||||
* in an `ar' header matches the pattern "/[0-9]*". The digits form a
|
||||
* There are two variants of the `ar' archive format: traditional BSD
|
||||
* and SVR4. These differ in the way long file names are treated, and
|
||||
* in the layout of the archive symbol table.
|
||||
*
|
||||
* The `ar' header only has space for a 16 character file name.
|
||||
*
|
||||
* In the SVR4 format, file names are terminated with a '/', so this
|
||||
* effectively leaves 15 characters for the actual file name. Longer
|
||||
* file names stored in a separate 'string table' and referenced
|
||||
* indirectly from the name field. The string table itself appears as
|
||||
* an archive member with name "// ". An `indirect' file name in an
|
||||
* `ar' header matches the pattern "/[0-9]*". The digits form a
|
||||
* decimal number that corresponds to a byte offset into the string
|
||||
* table where the actual file name of the object starts. Strings in
|
||||
* the string table are padded to start on even addresses.
|
||||
*
|
||||
* In the BSD format, file names can be upto 16 characters. File
|
||||
* names shorter than 16 characters are padded to 16 characters using
|
||||
* (ASCII) space characters. File names with embedded spaces and file
|
||||
* names longer than 16 characters are stored immediately after the
|
||||
* archive header and the name field set to a special indirect name
|
||||
* matching the pattern "#1/[0-9]+". The digits form a decimal number
|
||||
* that corresponds to the actual length of the file name following
|
||||
* the archive header. The content of the archive member immediately
|
||||
* follows the file name, and the size field of the archive member
|
||||
* holds the sum of the sizes of the member and of the appended file
|
||||
* name.
|
||||
*
|
||||
* Archives may also have a symbol table (see ranlib(1)), mapping
|
||||
* program symbols to object files inside the archive. A symbol table
|
||||
* uses a file name of "/ " in its archive header. The symbol table
|
||||
* is structured as:
|
||||
* program symbols to object files inside the archive.
|
||||
*
|
||||
* In the SVR4 format, a symbol table uses a file name of "/ " in its
|
||||
* archive header. The symbol table is structured as:
|
||||
* - a 4-byte count of entries stored as a binary value, MSB first
|
||||
* - 'n' 4-byte offsets, stored as binary values, MSB first
|
||||
* - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
|
||||
*
|
||||
* In the BSD format, the symbol table uses a file name of "__.SYMDEF".
|
||||
* It is structured as two parts:
|
||||
* - The first part is an array of "ranlib" structures preceded by
|
||||
* the size of the array in bytes. Each "ranlib" structure
|
||||
* describes one symbol. Each structure contains an offset into
|
||||
* the string table for the symbol name, and a file offset into the
|
||||
* archive for the member defining the symbol.
|
||||
* - The second part is a string table containing NUL-terminated
|
||||
* strings, preceded by the size of the string table in bytes.
|
||||
*
|
||||
* If the symbol table and string table are is present in an archive
|
||||
* they must be the very first objects and in that order.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Convert a string bounded by `start' and `start+sz' (exclusive) to a
|
||||
* number in the specified base.
|
||||
*/
|
||||
static int
|
||||
_libelf_ar_get_number(char *s, size_t sz, int base, size_t *ret)
|
||||
{
|
||||
int c, v;
|
||||
size_t r;
|
||||
char *e;
|
||||
|
||||
assert(base <= 10);
|
||||
|
||||
e = s + sz;
|
||||
|
||||
/* skip leading blanks */
|
||||
for (;s < e && (c = *s) == ' '; s++)
|
||||
;
|
||||
|
||||
r = 0L;
|
||||
for (;s < e; s++) {
|
||||
if ((c = *s) == ' ')
|
||||
break;
|
||||
if (c < '0' || c > '9')
|
||||
return (0);
|
||||
v = c - '0';
|
||||
if (v >= base) /* Illegal digit. */
|
||||
break;
|
||||
r *= base;
|
||||
r += v;
|
||||
}
|
||||
|
||||
*ret = r;
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve a string from a name field. If `rawname' is set, leave
|
||||
* ar(1) control characters in.
|
||||
* Retrieve an archive header descriptor.
|
||||
*/
|
||||
static char *
|
||||
_libelf_ar_get_string(const char *buf, size_t bufsize, int rawname)
|
||||
{
|
||||
const char *q;
|
||||
char *r;
|
||||
size_t sz;
|
||||
|
||||
if (rawname)
|
||||
sz = bufsize + 1;
|
||||
else {
|
||||
/* Skip back over trailing blanks. */
|
||||
for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
|
||||
;
|
||||
|
||||
if (q < buf) {
|
||||
/*
|
||||
* If the input buffer only had blanks in it,
|
||||
* return a zero-length string.
|
||||
*/
|
||||
buf = "";
|
||||
sz = 1;
|
||||
} else {
|
||||
/*
|
||||
* Remove the trailing '/' character, but only
|
||||
* if the name isn't one of the special names
|
||||
* "/" and "//".
|
||||
*/
|
||||
if (q > buf + 1 ||
|
||||
(q == (buf + 1) && *buf != '/'))
|
||||
q--;
|
||||
|
||||
sz = q - buf + 2; /* Space for a trailing NUL. */
|
||||
}
|
||||
}
|
||||
|
||||
if ((r = malloc(sz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
(void) strncpy(r, buf, sz);
|
||||
r[sz - 1] = '\0';
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve the full name of the archive member.
|
||||
*/
|
||||
static char *
|
||||
_libelf_ar_get_name(char *buf, size_t bufsize, Elf *e)
|
||||
{
|
||||
char c, *q, *r, *s;
|
||||
size_t len;
|
||||
size_t offset;
|
||||
|
||||
assert(e->e_kind == ELF_K_AR);
|
||||
|
||||
if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
|
||||
/*
|
||||
* The value in field ar_name is a decimal offset into
|
||||
* the archive string table where the actual name
|
||||
* resides.
|
||||
*/
|
||||
if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
|
||||
&offset) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (offset > e->e_u.e_ar.e_rawstrtabsz) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
s = q = e->e_u.e_ar.e_rawstrtab + offset;
|
||||
r = e->e_u.e_ar.e_rawstrtab + e->e_u.e_ar.e_rawstrtabsz;
|
||||
|
||||
for (s = q; s < r && *s != '/'; s++)
|
||||
;
|
||||
len = s - q + 1; /* space for the trailing NUL */
|
||||
|
||||
if ((s = malloc(len)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
(void) strncpy(s, q, len);
|
||||
s[len - 1] = '\0';
|
||||
|
||||
return (s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Normal 'name'
|
||||
*/
|
||||
return (_libelf_ar_get_string(buf, bufsize, 0));
|
||||
}
|
||||
|
||||
|
||||
Elf_Arhdr *
|
||||
_libelf_ar_gethdr(Elf *e)
|
||||
{
|
||||
Elf *parent;
|
||||
struct ar_hdr *arh;
|
||||
Elf_Arhdr *eh;
|
||||
size_t n;
|
||||
Elf *parent;
|
||||
Elf_Arhdr *eh;
|
||||
char *namelen;
|
||||
size_t n, nlen;
|
||||
struct ar_hdr *arh;
|
||||
|
||||
if ((parent = e->e_parent) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((parent = e->e_parent) == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
arh = (struct ar_hdr *) ((uintptr_t) e->e_rawfile - sizeof(struct ar_hdr));
|
||||
assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
|
||||
|
||||
assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
|
||||
assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + parent->e_rawsize -
|
||||
sizeof(struct ar_hdr));
|
||||
arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
|
||||
|
||||
if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
|
||||
assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile +
|
||||
parent->e_rawsize - sizeof(struct ar_hdr));
|
||||
|
||||
e->e_arhdr = eh;
|
||||
eh->ar_name = eh->ar_rawname = NULL;
|
||||
if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((eh->ar_name = _libelf_ar_get_name(arh->ar_name, sizeof(arh->ar_name),
|
||||
parent)) == NULL)
|
||||
goto error;
|
||||
e->e_hdr.e_arhdr = eh;
|
||||
e->e_flags |= LIBELF_F_AR_HEADER;
|
||||
|
||||
if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, &n) == 0)
|
||||
goto error;
|
||||
eh->ar_uid = (uid_t) n;
|
||||
eh->ar_name = eh->ar_rawname = NULL;
|
||||
|
||||
if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, &n) == 0)
|
||||
goto error;
|
||||
eh->ar_gid = (gid_t) n;
|
||||
if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) ==
|
||||
NULL)
|
||||
goto error;
|
||||
|
||||
if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, &n) == 0)
|
||||
goto error;
|
||||
eh->ar_mode = (mode_t) n;
|
||||
if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10,
|
||||
&n) == 0)
|
||||
goto error;
|
||||
eh->ar_uid = (uid_t) n;
|
||||
|
||||
if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &n) == 0)
|
||||
goto error;
|
||||
eh->ar_size = n;
|
||||
if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10,
|
||||
&n) == 0)
|
||||
goto error;
|
||||
eh->ar_gid = (gid_t) n;
|
||||
|
||||
if ((eh->ar_rawname = _libelf_ar_get_string(arh->ar_name,
|
||||
sizeof(arh->ar_name), 1)) == NULL)
|
||||
goto error;
|
||||
if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8,
|
||||
&n) == 0)
|
||||
goto error;
|
||||
eh->ar_mode = (mode_t) n;
|
||||
|
||||
return (eh);
|
||||
if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
|
||||
&n) == 0)
|
||||
goto error;
|
||||
|
||||
/*
|
||||
* Get the true size of the member if extended naming is being used.
|
||||
*/
|
||||
if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
|
||||
namelen = arh->ar_name +
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
|
||||
if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
|
||||
goto error;
|
||||
n -= nlen;
|
||||
}
|
||||
|
||||
eh->ar_size = n;
|
||||
|
||||
if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
|
||||
goto error;
|
||||
|
||||
eh->ar_flags = 0;
|
||||
|
||||
return (eh);
|
||||
|
||||
error:
|
||||
if (eh) {
|
||||
if (eh->ar_name)
|
||||
free(eh->ar_name);
|
||||
if (eh->ar_rawname)
|
||||
free(eh->ar_rawname);
|
||||
free(eh);
|
||||
}
|
||||
e->e_arhdr = NULL;
|
||||
if (eh) {
|
||||
if (eh->ar_name)
|
||||
free(eh->ar_name);
|
||||
if (eh->ar_rawname)
|
||||
free(eh->ar_rawname);
|
||||
free(eh);
|
||||
}
|
||||
|
||||
return (NULL);
|
||||
e->e_flags &= ~LIBELF_F_AR_HEADER;
|
||||
e->e_hdr.e_rawhdr = (unsigned char *) arh;
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
Elf *
|
||||
_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
|
||||
{
|
||||
Elf *e;
|
||||
off_t next;
|
||||
struct ar_hdr *arh;
|
||||
size_t sz;
|
||||
Elf *e;
|
||||
off_t next;
|
||||
size_t nsz, sz;
|
||||
struct ar_hdr *arh;
|
||||
char *member, *namelen;
|
||||
|
||||
assert(elf->e_kind == ELF_K_AR);
|
||||
assert(elf->e_kind == ELF_K_AR);
|
||||
|
||||
next = elf->e_u.e_ar.e_next;
|
||||
next = elf->e_u.e_ar.e_next;
|
||||
|
||||
/*
|
||||
* `next' is only set to zero by elf_next() when the last
|
||||
* member of an archive is processed.
|
||||
*/
|
||||
if (next == (off_t) 0)
|
||||
return (NULL);
|
||||
/*
|
||||
* `next' is only set to zero by elf_next() when the last
|
||||
* member of an archive is processed.
|
||||
*/
|
||||
if (next == (off_t) 0)
|
||||
return (NULL);
|
||||
|
||||
assert((next & 1) == 0);
|
||||
assert((next & 1) == 0);
|
||||
|
||||
arh = (struct ar_hdr *) (elf->e_rawfile + next);
|
||||
arh = (struct ar_hdr *) (elf->e_rawfile + next);
|
||||
|
||||
if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &sz) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
/*
|
||||
* Retrieve the size of the member.
|
||||
*/
|
||||
if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
|
||||
&sz) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(sz > 0);
|
||||
/*
|
||||
* Adjust the size field for members in BSD archives using
|
||||
* extended naming.
|
||||
*/
|
||||
if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
|
||||
namelen = arh->ar_name +
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
|
||||
if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
arh++; /* skip over archive member header */
|
||||
member = (char *) (arh + 1) + nsz;
|
||||
sz -= nsz;
|
||||
} else
|
||||
member = (char *) (arh + 1);
|
||||
|
||||
if ((e = elf_memory((char *) arh, sz)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
e->e_fd = fd;
|
||||
e->e_cmd = c;
|
||||
if ((e = elf_memory(member, sz)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
elf->e_u.e_ar.e_nchildren++;
|
||||
e->e_parent = elf;
|
||||
e->e_fd = fd;
|
||||
e->e_cmd = c;
|
||||
e->e_hdr.e_rawhdr = (unsigned char *) arh;
|
||||
|
||||
return (e);
|
||||
}
|
||||
elf->e_u.e_ar.e_nchildren++;
|
||||
e->e_parent = elf;
|
||||
|
||||
Elf *
|
||||
_libelf_ar_open(Elf *e)
|
||||
{
|
||||
int i;
|
||||
char *s, *end;
|
||||
size_t sz;
|
||||
struct ar_hdr arh;
|
||||
|
||||
e->e_kind = ELF_K_AR;
|
||||
e->e_u.e_ar.e_nchildren = 0;
|
||||
e->e_u.e_ar.e_next = (off_t) -1;
|
||||
|
||||
/*
|
||||
* Look for special members.
|
||||
*/
|
||||
|
||||
s = e->e_rawfile + SARMAG;
|
||||
end = e->e_rawfile + e->e_rawsize;
|
||||
|
||||
assert(e->e_rawsize > 0);
|
||||
|
||||
/*
|
||||
* Look for magic names "/ " and "// " in the first two entries
|
||||
* of the archive.
|
||||
*/
|
||||
for (i = 0; i < 2; i++) {
|
||||
|
||||
if (s + sizeof(arh) > end) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
(void) memcpy(&arh, s, sizeof(arh));
|
||||
|
||||
if (arh.ar_fmag[0] != '`' || arh.ar_fmag[1] != '\n') {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (arh.ar_name[0] != '/') /* not a special symbol */
|
||||
break;
|
||||
|
||||
if (_libelf_ar_get_number(arh.ar_size, sizeof(arh.ar_size), 10, &sz) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(sz > 0);
|
||||
|
||||
s += sizeof(arh);
|
||||
|
||||
if (arh.ar_name[1] == ' ') { /* "/ " => symbol table */
|
||||
|
||||
e->e_u.e_ar.e_rawsymtab = s;
|
||||
e->e_u.e_ar.e_rawsymtabsz = sz;
|
||||
|
||||
} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
|
||||
|
||||
/* "// " => string table for long file names */
|
||||
e->e_u.e_ar.e_rawstrtab = s;
|
||||
e->e_u.e_ar.e_rawstrtabsz = sz;
|
||||
}
|
||||
|
||||
sz = LIBELF_ADJUST_AR_SIZE(sz);
|
||||
|
||||
s += sz;
|
||||
}
|
||||
|
||||
e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
|
||||
|
||||
return (e);
|
||||
return (e);
|
||||
}
|
||||
|
||||
/*
|
||||
* An ar(1) symbol table has the following layout:
|
||||
* A BSD-style ar(1) symbol table has the following layout:
|
||||
*
|
||||
* The first 4 bytes are a binary count of the number of entries in the
|
||||
* symbol table, stored MSB-first.
|
||||
* - A count of bytes used by the following array of 'ranlib'
|
||||
* structures, stored as a 'long'.
|
||||
* - An array of 'ranlib' structures. Each array element is
|
||||
* two 'long's in size.
|
||||
* - A count of bytes used for the following symbol table.
|
||||
* - The symbol table itself.
|
||||
*/
|
||||
|
||||
/*
|
||||
* A helper macro to read in a 'long' value from the archive.
|
||||
*
|
||||
* Then there are 'n' 4-byte binary offsets, also stored MSB first.
|
||||
* We use memcpy() since the source pointer may be misaligned with
|
||||
* respect to the natural alignment for a C 'long'.
|
||||
*/
|
||||
#define GET_LONG(P, V)do { \
|
||||
memcpy(&(V), (P), sizeof(long)); \
|
||||
(P) += sizeof(long); \
|
||||
} while (0)
|
||||
|
||||
Elf_Arsym *
|
||||
_libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
|
||||
{
|
||||
Elf_Arsym *symtab, *sym;
|
||||
unsigned int n, nentries;
|
||||
unsigned char *end, *p, *p0, *s, *s0;
|
||||
const size_t entrysize = 2 * sizeof(long);
|
||||
long arraysize, fileoffset, stroffset, strtabsize;
|
||||
|
||||
assert(e != NULL);
|
||||
assert(count != NULL);
|
||||
assert(e->e_u.e_ar.e_symtab == NULL);
|
||||
|
||||
symtab = NULL;
|
||||
|
||||
/*
|
||||
* The BSD symbol table always contains the count fields even
|
||||
* if there are no entries in it.
|
||||
*/
|
||||
if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
|
||||
goto symtaberror;
|
||||
|
||||
p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
|
||||
end = p0 + e->e_u.e_ar.e_rawsymtabsz;
|
||||
|
||||
/*
|
||||
* Retrieve the size of the array of ranlib descriptors and
|
||||
* check it for validity.
|
||||
*/
|
||||
GET_LONG(p, arraysize);
|
||||
|
||||
if (arraysize < 0 || p0 + arraysize >= end ||
|
||||
((size_t) arraysize % entrysize != 0))
|
||||
goto symtaberror;
|
||||
|
||||
/*
|
||||
* Check the value of the string table size.
|
||||
*/
|
||||
s = p + arraysize;
|
||||
GET_LONG(s, strtabsize);
|
||||
|
||||
s0 = s; /* Start of string table. */
|
||||
if (strtabsize < 0 || s0 + strtabsize > end)
|
||||
goto symtaberror;
|
||||
|
||||
nentries = (size_t) arraysize / entrysize;
|
||||
|
||||
/*
|
||||
* Allocate space for the returned Elf_Arsym array.
|
||||
*/
|
||||
if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* Read in symbol table entries. */
|
||||
for (n = 0, sym = symtab; n < nentries; n++, sym++) {
|
||||
GET_LONG(p, stroffset);
|
||||
GET_LONG(p, fileoffset);
|
||||
|
||||
if (stroffset < 0 || fileoffset < 0 ||
|
||||
(size_t) fileoffset >= e->e_rawsize)
|
||||
goto symtaberror;
|
||||
|
||||
s = s0 + stroffset;
|
||||
|
||||
if (s >= end)
|
||||
goto symtaberror;
|
||||
|
||||
sym->as_off = (off_t) fileoffset;
|
||||
sym->as_hash = elf_hash((char *) s);
|
||||
sym->as_name = (char *) s;
|
||||
}
|
||||
|
||||
/* Fill up the sentinel entry. */
|
||||
sym->as_name = NULL;
|
||||
sym->as_hash = ~0UL;
|
||||
sym->as_off = (off_t) 0;
|
||||
|
||||
/* Remember the processed symbol table. */
|
||||
e->e_u.e_ar.e_symtab = symtab;
|
||||
|
||||
*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
|
||||
|
||||
return (symtab);
|
||||
|
||||
symtaberror:
|
||||
if (symtab)
|
||||
free(symtab);
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* An SVR4-style ar(1) symbol table has the following layout:
|
||||
*
|
||||
* Following this, there are 'n' null-terminated strings.
|
||||
* - The first 4 bytes are a binary count of the number of entries in the
|
||||
* symbol table, stored MSB-first.
|
||||
* - Then there are 'n' 4-byte binary offsets, also stored MSB first.
|
||||
* - Following this, there are 'n' null-terminated strings.
|
||||
*/
|
||||
|
||||
#define GET_WORD(P, V) do { \
|
||||
(V) = 0; \
|
||||
(V) = (P)[0]; (V) <<= 8; \
|
||||
(V) += (P)[1]; (V) <<= 8; \
|
||||
(V) += (P)[2]; (V) <<= 8; \
|
||||
(V) += (P)[3]; \
|
||||
} while (0)
|
||||
(V) = 0; \
|
||||
(V) = (P)[0]; (V) <<= 8; \
|
||||
(V) += (P)[1]; (V) <<= 8; \
|
||||
(V) += (P)[2]; (V) <<= 8; \
|
||||
(V) += (P)[3]; \
|
||||
} while (0)
|
||||
|
||||
#define INTSZ 4
|
||||
|
||||
|
||||
Elf_Arsym *
|
||||
_libelf_ar_process_symtab(Elf *e, size_t *count)
|
||||
_libelf_ar_process_svr4_symtab(Elf *e, size_t *count)
|
||||
{
|
||||
size_t n, nentries, off;
|
||||
Elf_Arsym *symtab, *sym;
|
||||
char *p, *s, *end;
|
||||
uint32_t off;
|
||||
size_t n, nentries;
|
||||
Elf_Arsym *symtab, *sym;
|
||||
unsigned char *p, *s, *end;
|
||||
|
||||
assert(e != NULL);
|
||||
assert(count != NULL);
|
||||
assert(e != NULL);
|
||||
assert(count != NULL);
|
||||
assert(e->e_u.e_ar.e_symtab == NULL);
|
||||
|
||||
if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
symtab = NULL;
|
||||
|
||||
p = e->e_u.e_ar.e_rawsymtab;
|
||||
end = p + e->e_u.e_ar.e_rawsymtabsz;
|
||||
if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
|
||||
goto symtaberror;
|
||||
|
||||
GET_WORD(p, nentries);
|
||||
p += INTSZ;
|
||||
p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
|
||||
end = p + e->e_u.e_ar.e_rawsymtabsz;
|
||||
|
||||
if (nentries == 0 || p + nentries * INTSZ >= end) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
GET_WORD(p, nentries);
|
||||
p += INTSZ;
|
||||
|
||||
/* Allocate space for a nentries + a sentinel. */
|
||||
if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (nentries == 0 || p + nentries * INTSZ >= end)
|
||||
goto symtaberror;
|
||||
|
||||
s = p + (nentries * INTSZ); /* start of the string table. */
|
||||
/* Allocate space for a nentries + a sentinel. */
|
||||
if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
for (n = nentries, sym = symtab; n > 0; n--) {
|
||||
off = 0;
|
||||
s = p + (nentries * INTSZ); /* start of the string table. */
|
||||
|
||||
GET_WORD(p, off);
|
||||
for (n = nentries, sym = symtab; n > 0; n--) {
|
||||
if (s >= end)
|
||||
goto symtaberror;
|
||||
|
||||
sym->as_off = off;
|
||||
sym->as_hash = elf_hash(s);
|
||||
sym->as_name = s;
|
||||
GET_WORD(p, off);
|
||||
if (off >= e->e_rawsize)
|
||||
goto symtaberror;
|
||||
|
||||
p += INTSZ;
|
||||
sym++;
|
||||
sym->as_off = (off_t) off;
|
||||
sym->as_hash = elf_hash((char *) s);
|
||||
sym->as_name = (char *) s;
|
||||
|
||||
for (; s < end && *s++ != '\0';) /* skip to next string */
|
||||
;
|
||||
if (s > end) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
free(symtab);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
p += INTSZ;
|
||||
sym++;
|
||||
|
||||
/* Fill up the sentinel entry. */
|
||||
sym->as_name = NULL;
|
||||
sym->as_hash = ~0UL;
|
||||
sym->as_off = (off_t) 0;
|
||||
for (; s < end && *s++ != '\0';) /* skip to next string */
|
||||
;
|
||||
}
|
||||
|
||||
*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
|
||||
e->e_u.e_ar.e_symtab = symtab;
|
||||
/* Fill up the sentinel entry. */
|
||||
sym->as_name = NULL;
|
||||
sym->as_hash = ~0UL;
|
||||
sym->as_off = (off_t) 0;
|
||||
|
||||
return (symtab);
|
||||
*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
|
||||
e->e_u.e_ar.e_symtab = symtab;
|
||||
|
||||
return (symtab);
|
||||
|
||||
symtaberror:
|
||||
if (symtab)
|
||||
free(symtab);
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
360
ext/libelf/libelf_ar_util.c
Normal file
360
ext/libelf/libelf_ar_util.c
Normal file
@@ -0,0 +1,360 @@
|
||||
/*-
|
||||
* Copyright (c) 2006,2009,2010 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
#include "_libelf_ar.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_ar_util.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/*
|
||||
* Convert a string bounded by `start' and `start+sz' (exclusive) to a
|
||||
* number in the specified base.
|
||||
*/
|
||||
int
|
||||
_libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
|
||||
size_t *ret)
|
||||
{
|
||||
size_t r;
|
||||
unsigned int c, v;
|
||||
const unsigned char *e, *s;
|
||||
|
||||
assert(base <= 10);
|
||||
|
||||
s = (const unsigned char *) src;
|
||||
e = s + sz;
|
||||
|
||||
/* skip leading blanks */
|
||||
for (;s < e && (c = *s) == ' '; s++)
|
||||
;
|
||||
|
||||
r = 0L;
|
||||
for (;s < e; s++) {
|
||||
if ((c = *s) == ' ')
|
||||
break;
|
||||
if (c < '0' || c > '9')
|
||||
return (0);
|
||||
v = c - '0';
|
||||
if (v >= base) /* Illegal digit. */
|
||||
break;
|
||||
r *= base;
|
||||
r += v;
|
||||
}
|
||||
|
||||
*ret = r;
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the translated name for an archive member.
|
||||
*/
|
||||
char *
|
||||
_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
|
||||
{
|
||||
char *s;
|
||||
unsigned char c;
|
||||
size_t len, offset;
|
||||
const unsigned char *buf, *p, *q, *r;
|
||||
const size_t bufsize = sizeof(arh->ar_name);
|
||||
|
||||
assert(arh != NULL);
|
||||
assert(ar->e_kind == ELF_K_AR);
|
||||
assert((const unsigned char *) arh >= ar->e_rawfile &&
|
||||
(const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
|
||||
|
||||
buf = (const unsigned char *) arh->ar_name;
|
||||
|
||||
/*
|
||||
* Check for extended naming.
|
||||
*
|
||||
* If the name matches the pattern "^/[0-9]+", it is an
|
||||
* SVR4-style extended name. If the name matches the pattern
|
||||
* "#1/[0-9]+", the entry uses BSD style extended naming.
|
||||
*/
|
||||
if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
|
||||
/*
|
||||
* The value in field ar_name is a decimal offset into
|
||||
* the archive string table where the actual name
|
||||
* resides.
|
||||
*/
|
||||
if (_libelf_ar_get_number((const char *) (buf + 1),
|
||||
bufsize - 1, 10, &offset) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
p = q = ar->e_u.e_ar.e_rawstrtab + offset;
|
||||
r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
|
||||
|
||||
for (; p < r && *p != '/'; p++)
|
||||
;
|
||||
len = (size_t) (p - q + 1); /* space for the trailing NUL */
|
||||
|
||||
if ((s = malloc(len)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
(void) strncpy(s, (const char *) q, len - 1);
|
||||
s[len - 1] = '\0';
|
||||
|
||||
return (s);
|
||||
} else if (IS_EXTENDED_BSD_NAME(buf)) {
|
||||
r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
|
||||
|
||||
if (_libelf_ar_get_number((const char *) r, bufsize -
|
||||
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
|
||||
&len) == 0) {
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate space for the file name plus a
|
||||
* trailing NUL.
|
||||
*/
|
||||
if ((s = malloc(len + 1)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* The file name follows the archive header.
|
||||
*/
|
||||
q = (const unsigned char *) (arh + 1);
|
||||
|
||||
(void) strncpy(s, (const char *) q, len);
|
||||
s[len] = '\0';
|
||||
|
||||
return (s);
|
||||
}
|
||||
|
||||
/*
|
||||
* A 'normal' name.
|
||||
*
|
||||
* Skip back over trailing blanks from the end of the field.
|
||||
* In the SVR4 format, a '/' is used as a terminator for
|
||||
* non-special names.
|
||||
*/
|
||||
for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
|
||||
;
|
||||
|
||||
if (q >= buf) {
|
||||
if (*q == '/') {
|
||||
/*
|
||||
* SVR4 style names: ignore the trailing
|
||||
* character '/', but only if the name is not
|
||||
* one of the special names "/" and "//".
|
||||
*/
|
||||
if (q > buf + 1 ||
|
||||
(q == (buf + 1) && *buf != '/'))
|
||||
q--;
|
||||
}
|
||||
|
||||
len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
|
||||
} else {
|
||||
/* The buffer only had blanks. */
|
||||
buf = (const unsigned char *) "";
|
||||
len = 1;
|
||||
}
|
||||
|
||||
if ((s = malloc(len)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
(void) strncpy(s, (const char *) buf, len - 1);
|
||||
s[len - 1] = '\0';
|
||||
|
||||
return (s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the raw name for an archive member, inclusive of any
|
||||
* formatting characters.
|
||||
*/
|
||||
char *
|
||||
_libelf_ar_get_raw_name(const struct ar_hdr *arh)
|
||||
{
|
||||
char *rawname;
|
||||
const size_t namesz = sizeof(arh->ar_name);
|
||||
|
||||
if ((rawname = malloc(namesz + 1)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
(void) strncpy(rawname, arh->ar_name, namesz);
|
||||
rawname[namesz] = '\0';
|
||||
return (rawname);
|
||||
}
|
||||
|
||||
/*
|
||||
* Open an 'ar' archive.
|
||||
*/
|
||||
Elf *
|
||||
_libelf_ar_open(Elf *e, int reporterror)
|
||||
{
|
||||
size_t sz;
|
||||
int scanahead;
|
||||
struct ar_hdr arh;
|
||||
unsigned char *s, *end;
|
||||
|
||||
_libelf_init_elf(e, ELF_K_AR);
|
||||
|
||||
e->e_u.e_ar.e_nchildren = 0;
|
||||
e->e_u.e_ar.e_next = (off_t) -1;
|
||||
|
||||
/*
|
||||
* Look for special members.
|
||||
*/
|
||||
|
||||
s = e->e_rawfile + SARMAG;
|
||||
end = e->e_rawfile + e->e_rawsize;
|
||||
|
||||
assert(e->e_rawsize > 0);
|
||||
|
||||
/*
|
||||
* We use heuristics to determine the flavor of the archive we
|
||||
* are examining.
|
||||
*
|
||||
* SVR4 flavor archives use the name "/ " and "// " for
|
||||
* special members.
|
||||
*
|
||||
* In BSD flavor archives the symbol table, if present, is the
|
||||
* first archive with name "__.SYMDEF".
|
||||
*/
|
||||
|
||||
#define READ_AR_HEADER(S, ARH, SZ, END) \
|
||||
do { \
|
||||
if ((S) + sizeof((ARH)) > (END)) \
|
||||
goto error; \
|
||||
(void) memcpy(&(ARH), (S), sizeof((ARH))); \
|
||||
if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
|
||||
goto error; \
|
||||
if (_libelf_ar_get_number((char *) (ARH).ar_size, \
|
||||
sizeof((ARH).ar_size), 10, &(SZ)) == 0) \
|
||||
goto error; \
|
||||
} while (0)
|
||||
|
||||
READ_AR_HEADER(s, arh, sz, end);
|
||||
|
||||
/*
|
||||
* Handle special archive members for the SVR4 format.
|
||||
*/
|
||||
if (arh.ar_name[0] == '/') {
|
||||
if (sz == 0)
|
||||
goto error;
|
||||
|
||||
e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
|
||||
|
||||
scanahead = 0;
|
||||
|
||||
/*
|
||||
* The symbol table (file name "/ ") always comes before the
|
||||
* string table (file name "// ").
|
||||
*/
|
||||
if (arh.ar_name[1] == ' ') {
|
||||
/* "/ " => symbol table. */
|
||||
scanahead = 1; /* The string table to follow. */
|
||||
|
||||
s += sizeof(arh);
|
||||
e->e_u.e_ar.e_rawsymtab = s;
|
||||
e->e_u.e_ar.e_rawsymtabsz = sz;
|
||||
|
||||
sz = LIBELF_ADJUST_AR_SIZE(sz);
|
||||
s += sz;
|
||||
|
||||
} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
|
||||
/* "// " => string table for long file names. */
|
||||
s += sizeof(arh);
|
||||
e->e_u.e_ar.e_rawstrtab = s;
|
||||
e->e_u.e_ar.e_rawstrtabsz = sz;
|
||||
|
||||
sz = LIBELF_ADJUST_AR_SIZE(sz);
|
||||
s += sz;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the string table hasn't been seen yet, look for
|
||||
* it in the next member.
|
||||
*/
|
||||
if (scanahead) {
|
||||
READ_AR_HEADER(s, arh, sz, end);
|
||||
|
||||
/* "// " => string table for long file names. */
|
||||
if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
|
||||
arh.ar_name[2] == ' ') {
|
||||
|
||||
s += sizeof(arh);
|
||||
|
||||
e->e_u.e_ar.e_rawstrtab = s;
|
||||
e->e_u.e_ar.e_rawstrtabsz = sz;
|
||||
|
||||
sz = LIBELF_ADJUST_AR_SIZE(sz);
|
||||
s += sz;
|
||||
}
|
||||
}
|
||||
} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
|
||||
sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
|
||||
/*
|
||||
* BSD style archive symbol table.
|
||||
*/
|
||||
s += sizeof(arh);
|
||||
e->e_u.e_ar.e_rawsymtab = s;
|
||||
e->e_u.e_ar.e_rawsymtabsz = sz;
|
||||
|
||||
sz = LIBELF_ADJUST_AR_SIZE(sz);
|
||||
s += sz;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update the 'next' offset, so that a subsequent elf_begin()
|
||||
* works as expected.
|
||||
*/
|
||||
e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
|
||||
|
||||
return (e);
|
||||
|
||||
error:
|
||||
if (!reporterror) {
|
||||
e->e_kind = ELF_K_NONE;
|
||||
return (e);
|
||||
}
|
||||
|
||||
LIBELF_SET_ERROR(ARCHIVE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,73 +24,75 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gelf.h"
|
||||
#include <gelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_checksum.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
static unsigned long
|
||||
_libelf_sum(unsigned long c, const unsigned char *s, size_t size)
|
||||
{
|
||||
if (s == NULL || size == 0)
|
||||
return (c);
|
||||
if (s == NULL || size == 0)
|
||||
return (c);
|
||||
|
||||
while (size--)
|
||||
c += *s++;
|
||||
while (size--)
|
||||
c += *s++;
|
||||
|
||||
return (c);
|
||||
return (c);
|
||||
}
|
||||
|
||||
unsigned long
|
||||
long
|
||||
_libelf_checksum(Elf *e, int elfclass)
|
||||
{
|
||||
size_t shn;
|
||||
Elf_Scn *scn;
|
||||
Elf_Data *d;
|
||||
unsigned long checksum;
|
||||
GElf_Ehdr eh;
|
||||
GElf_Shdr shdr;
|
||||
size_t shn;
|
||||
Elf_Scn *scn;
|
||||
Elf_Data *d;
|
||||
unsigned long checksum;
|
||||
GElf_Ehdr eh;
|
||||
GElf_Shdr shdr;
|
||||
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0L);
|
||||
}
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (0L);
|
||||
}
|
||||
|
||||
if (e->e_class != elfclass) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (0L);
|
||||
}
|
||||
if (e->e_class != elfclass) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (0L);
|
||||
}
|
||||
|
||||
if (gelf_getehdr(e, &eh) == NULL)
|
||||
return (0);
|
||||
if (gelf_getehdr(e, &eh) == NULL)
|
||||
return (0);
|
||||
|
||||
/*
|
||||
* Iterate over all sections in the ELF file, computing the
|
||||
* checksum along the way.
|
||||
*
|
||||
* The first section is always SHN_UNDEF and can be skipped.
|
||||
* Non-allocatable sections are skipped, as are sections that
|
||||
* could be affected by utilities such as strip(1).
|
||||
*/
|
||||
/*
|
||||
* Iterate over all sections in the ELF file, computing the
|
||||
* checksum along the way.
|
||||
*
|
||||
* The first section is always SHN_UNDEF and can be skipped.
|
||||
* Non-allocatable sections are skipped, as are sections that
|
||||
* could be affected by utilities such as strip(1).
|
||||
*/
|
||||
|
||||
checksum = 0;
|
||||
for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) {
|
||||
if ((scn = elf_getscn(e, shn)) == NULL)
|
||||
return (0);
|
||||
if (gelf_getshdr(scn, &shdr) == NULL)
|
||||
return (0);
|
||||
if ((shdr.sh_flags & SHF_ALLOC) == 0 ||
|
||||
shdr.sh_type == SHT_DYNAMIC ||
|
||||
shdr.sh_type == SHT_DYNSYM)
|
||||
continue;
|
||||
checksum = 0;
|
||||
for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) {
|
||||
if ((scn = elf_getscn(e, shn)) == NULL)
|
||||
return (0);
|
||||
if (gelf_getshdr(scn, &shdr) == NULL)
|
||||
return (0);
|
||||
if ((shdr.sh_flags & SHF_ALLOC) == 0 ||
|
||||
shdr.sh_type == SHT_DYNAMIC ||
|
||||
shdr.sh_type == SHT_DYNSYM)
|
||||
continue;
|
||||
|
||||
d = NULL;
|
||||
while ((d = elf_rawdata(scn, d)) != NULL)
|
||||
checksum = _libelf_sum(checksum,
|
||||
(unsigned char *) d->d_buf, d->d_size);
|
||||
}
|
||||
d = NULL;
|
||||
while ((d = elf_rawdata(scn, d)) != NULL)
|
||||
checksum = _libelf_sum(checksum,
|
||||
(unsigned char *) d->d_buf, (size_t) d->d_size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a 16-bit checksum compatible with Solaris.
|
||||
*/
|
||||
return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
|
||||
/*
|
||||
* Return a 16-bit checksum compatible with Solaris.
|
||||
*/
|
||||
return (long) (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,55 +24,78 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_data.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
int
|
||||
_libelf_xlate_shtype(uint32_t sht)
|
||||
{
|
||||
switch (sht) {
|
||||
case SHT_DYNAMIC:
|
||||
return (ELF_T_DYN);
|
||||
case SHT_DYNSYM:
|
||||
return (ELF_T_SYM);
|
||||
case SHT_FINI_ARRAY:
|
||||
return (ELF_T_ADDR);
|
||||
case SHT_GROUP:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_HASH:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_INIT_ARRAY:
|
||||
return (ELF_T_ADDR);
|
||||
case SHT_NOBITS:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_NOTE:
|
||||
return (ELF_T_NOTE);
|
||||
case SHT_PREINIT_ARRAY:
|
||||
return (ELF_T_ADDR);
|
||||
case SHT_PROGBITS:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_REL:
|
||||
return (ELF_T_REL);
|
||||
case SHT_RELA:
|
||||
return (ELF_T_RELA);
|
||||
case SHT_STRTAB:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_SYMTAB:
|
||||
return (ELF_T_SYM);
|
||||
case SHT_SYMTAB_SHNDX:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_GNU_verdef: /* == SHT_SUNW_verdef */
|
||||
return (ELF_T_VDEF);
|
||||
case SHT_GNU_verneed: /* == SHT_SUNW_verneed */
|
||||
return (ELF_T_VNEED);
|
||||
case SHT_GNU_versym: /* == SHT_SUNW_versym */
|
||||
return (-1); /* XXX */
|
||||
case SHT_SUNW_move:
|
||||
return (ELF_T_MOVE);
|
||||
case SHT_SUNW_syminfo:
|
||||
return (ELF_T_SYMINFO);
|
||||
default:
|
||||
return (-1);
|
||||
}
|
||||
/*
|
||||
* Look for known section types.
|
||||
*/
|
||||
switch (sht) {
|
||||
case SHT_DYNAMIC:
|
||||
return (ELF_T_DYN);
|
||||
case SHT_DYNSYM:
|
||||
return (ELF_T_SYM);
|
||||
case SHT_FINI_ARRAY:
|
||||
return (ELF_T_ADDR);
|
||||
case SHT_GNU_HASH:
|
||||
return (ELF_T_GNUHASH);
|
||||
case SHT_GNU_LIBLIST:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_GROUP:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_HASH:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_INIT_ARRAY:
|
||||
return (ELF_T_ADDR);
|
||||
case SHT_NOBITS:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_NOTE:
|
||||
return (ELF_T_NOTE);
|
||||
case SHT_PREINIT_ARRAY:
|
||||
return (ELF_T_ADDR);
|
||||
case SHT_PROGBITS:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_REL:
|
||||
return (ELF_T_REL);
|
||||
case SHT_RELA:
|
||||
return (ELF_T_RELA);
|
||||
case SHT_STRTAB:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_SYMTAB:
|
||||
return (ELF_T_SYM);
|
||||
case SHT_SYMTAB_SHNDX:
|
||||
return (ELF_T_WORD);
|
||||
case SHT_SUNW_dof:
|
||||
return (ELF_T_BYTE);
|
||||
case SHT_SUNW_move:
|
||||
return (ELF_T_MOVE);
|
||||
case SHT_SUNW_syminfo:
|
||||
return (ELF_T_SYMINFO);
|
||||
case SHT_SUNW_verdef: /* == SHT_GNU_verdef */
|
||||
return (ELF_T_VDEF);
|
||||
case SHT_SUNW_verneed: /* == SHT_GNU_verneed */
|
||||
return (ELF_T_VNEED);
|
||||
case SHT_SUNW_versym: /* == SHT_GNU_versym */
|
||||
return (ELF_T_HALF);
|
||||
default:
|
||||
/*
|
||||
* Values in the range [SHT_LOOS..SHT_HIUSER] (i.e.,
|
||||
* OS, processor and user-defined section types) are
|
||||
* legal, but since we do not know anything more about
|
||||
* their semantics, we return a type of ELF_T_BYTE.
|
||||
*/
|
||||
if (sht >= SHT_LOOS && sht <= SHT_HIUSER)
|
||||
return (ELF_T_BYTE);
|
||||
|
||||
/*
|
||||
* Other values are unsupported.
|
||||
*/
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -22,17 +22,17 @@
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/libelf_ehdr.c,v 1.2 2006/12/25 02:22:22 jkoshy Exp $
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_ehdr.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/*
|
||||
* Retrieve counts for sections, phdrs and the section string table index
|
||||
* from section header #0 of the ELF object.
|
||||
@@ -41,161 +41,164 @@ static int
|
||||
_libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
|
||||
uint16_t strndx)
|
||||
{
|
||||
Elf_Scn *scn;
|
||||
size_t fsz;
|
||||
void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
|
||||
uint32_t shtype;
|
||||
Elf_Scn *scn;
|
||||
size_t fsz;
|
||||
int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
|
||||
size_t _c, int _swap);
|
||||
uint32_t shtype;
|
||||
|
||||
assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
|
||||
assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
|
||||
|
||||
fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
|
||||
assert(fsz > 0);
|
||||
fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
|
||||
assert(fsz > 0);
|
||||
|
||||
if (e->e_rawsize < shoff + fsz) { /* raw file too small */
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (0);
|
||||
}
|
||||
if (e->e_rawsize < shoff + fsz) { /* raw file too small */
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
|
||||
return (0);
|
||||
if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
|
||||
return (0);
|
||||
|
||||
xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
|
||||
(*xlator)((char *) &scn->s_shdr, e->e_rawfile + shoff, (size_t) 1,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder));
|
||||
xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
|
||||
(*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
|
||||
(unsigned char *) e->e_rawfile + shoff, (size_t) 1,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder));
|
||||
|
||||
#define GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
|
||||
scn->s_shdr.s_shdr64.M)
|
||||
scn->s_shdr.s_shdr64.M)
|
||||
|
||||
if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (0);
|
||||
}
|
||||
if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
|
||||
LIBELF_SET_ERROR(SECTION, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
e->e_u.e_elf.e_nscn = GET_SHDR_MEMBER(sh_size);
|
||||
e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
|
||||
GET_SHDR_MEMBER(sh_info);
|
||||
e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
|
||||
GET_SHDR_MEMBER(sh_link);
|
||||
e->e_u.e_elf.e_nscn = (size_t) GET_SHDR_MEMBER(sh_size);
|
||||
e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
|
||||
GET_SHDR_MEMBER(sh_info);
|
||||
e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
|
||||
GET_SHDR_MEMBER(sh_link);
|
||||
#undef GET_SHDR_MEMBER
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
#define EHDR_INIT(E,SZ) do { \
|
||||
Elf##SZ##_Ehdr *eh = (E); \
|
||||
eh->e_ident[EI_MAG0] = ELFMAG0; \
|
||||
eh->e_ident[EI_MAG1] = ELFMAG1; \
|
||||
eh->e_ident[EI_MAG2] = ELFMAG2; \
|
||||
eh->e_ident[EI_MAG3] = ELFMAG3; \
|
||||
eh->e_ident[EI_CLASS] = ELFCLASS##SZ; \
|
||||
eh->e_ident[EI_DATA] = ELFDATANONE; \
|
||||
eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version); \
|
||||
eh->e_machine = EM_NONE; \
|
||||
eh->e_type = ELF_K_NONE; \
|
||||
eh->e_version = LIBELF_PRIVATE(version); \
|
||||
} while (0)
|
||||
Elf##SZ##_Ehdr *eh = (E); \
|
||||
eh->e_ident[EI_MAG0] = ELFMAG0; \
|
||||
eh->e_ident[EI_MAG1] = ELFMAG1; \
|
||||
eh->e_ident[EI_MAG2] = ELFMAG2; \
|
||||
eh->e_ident[EI_MAG3] = ELFMAG3; \
|
||||
eh->e_ident[EI_CLASS] = ELFCLASS##SZ; \
|
||||
eh->e_ident[EI_DATA] = ELFDATANONE; \
|
||||
eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version) & 0xFFU; \
|
||||
eh->e_machine = EM_NONE; \
|
||||
eh->e_type = ELF_K_NONE; \
|
||||
eh->e_version = LIBELF_PRIVATE(version); \
|
||||
} while (0)
|
||||
|
||||
void *
|
||||
_libelf_ehdr(Elf *e, int ec, int allocate)
|
||||
{
|
||||
void *ehdr;
|
||||
size_t fsz, msz;
|
||||
uint16_t phnum, shnum, strndx;
|
||||
uint64_t shoff;
|
||||
void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
|
||||
void *ehdr;
|
||||
size_t fsz, msz;
|
||||
uint16_t phnum, shnum, strndx;
|
||||
uint64_t shoff;
|
||||
int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
|
||||
size_t _c, int _swap);
|
||||
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL || e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (e->e_version != EV_CURRENT) {
|
||||
LIBELF_SET_ERROR(VERSION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e->e_version != EV_CURRENT) {
|
||||
LIBELF_SET_ERROR(VERSION, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (e->e_class == ELFCLASSNONE)
|
||||
e->e_class = ec;
|
||||
if (e->e_class == ELFCLASSNONE)
|
||||
e->e_class = ec;
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
|
||||
else
|
||||
ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
|
||||
if (ec == ELFCLASS32)
|
||||
ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
|
||||
else
|
||||
ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
|
||||
|
||||
if (ehdr != NULL) /* already have a translated ehdr */
|
||||
return (ehdr);
|
||||
if (ehdr != NULL) /* already have a translated ehdr */
|
||||
return (ehdr);
|
||||
|
||||
fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
|
||||
assert(fsz > 0);
|
||||
fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
|
||||
assert(fsz > 0);
|
||||
|
||||
if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < fsz) {
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < fsz) {
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT);
|
||||
msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(msz > 0);
|
||||
|
||||
if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
|
||||
EHDR_INIT(ehdr,32);
|
||||
} else {
|
||||
e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
|
||||
EHDR_INIT(ehdr,64);
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
|
||||
EHDR_INIT(ehdr,32);
|
||||
} else {
|
||||
e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
|
||||
EHDR_INIT(ehdr,64);
|
||||
}
|
||||
|
||||
if (allocate)
|
||||
e->e_flags |= ELF_F_DIRTY;
|
||||
if (allocate)
|
||||
e->e_flags |= ELF_F_DIRTY;
|
||||
|
||||
if (e->e_cmd == ELF_C_WRITE)
|
||||
return (ehdr);
|
||||
if (e->e_cmd == ELF_C_WRITE)
|
||||
return (ehdr);
|
||||
|
||||
xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
|
||||
(*xlator)(ehdr, e->e_rawfile, (size_t) 1,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder));
|
||||
xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
|
||||
(*xlator)((unsigned char*) ehdr, msz, e->e_rawfile, (size_t) 1,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder));
|
||||
|
||||
/*
|
||||
* If extended numbering is being used, read the correct
|
||||
* number of sections and program header entries.
|
||||
*/
|
||||
if (ec == ELFCLASS32) {
|
||||
phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
|
||||
shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
|
||||
shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
|
||||
strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
|
||||
} else {
|
||||
phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
|
||||
shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
|
||||
shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
|
||||
strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
|
||||
}
|
||||
/*
|
||||
* If extended numbering is being used, read the correct
|
||||
* number of sections and program header entries.
|
||||
*/
|
||||
if (ec == ELFCLASS32) {
|
||||
phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
|
||||
shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
|
||||
shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
|
||||
strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
|
||||
} else {
|
||||
phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
|
||||
shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
|
||||
shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
|
||||
strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
|
||||
}
|
||||
|
||||
if (shnum >= SHN_LORESERVE ||
|
||||
(shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
|
||||
strndx == SHN_XINDEX))) {
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (shnum >= SHN_LORESERVE ||
|
||||
(shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
|
||||
strndx == SHN_XINDEX))) {
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */
|
||||
e->e_u.e_elf.e_nphdr = phnum;
|
||||
e->e_u.e_elf.e_nscn = shnum;
|
||||
e->e_u.e_elf.e_strndx = strndx;
|
||||
} else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
|
||||
return (NULL);
|
||||
if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */
|
||||
e->e_u.e_elf.e_nphdr = phnum;
|
||||
e->e_u.e_elf.e_nscn = shnum;
|
||||
e->e_u.e_elf.e_strndx = strndx;
|
||||
} else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
|
||||
return (NULL);
|
||||
|
||||
return (ehdr);
|
||||
return (ehdr);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -25,109 +25,110 @@
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_extended.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/*
|
||||
* Retrieve section #0, allocating a new section if needed.
|
||||
*/
|
||||
static Elf_Scn *
|
||||
_libelf_getscn0(Elf *e)
|
||||
{
|
||||
Elf_Scn *s;
|
||||
Elf_Scn *s;
|
||||
|
||||
if ((s = STAILQ_FIRST(&e->e_u.e_elf.e_scn)) != NULL)
|
||||
return (s);
|
||||
if ((s = STAILQ_FIRST(&e->e_u.e_elf.e_scn)) != NULL)
|
||||
return (s);
|
||||
|
||||
return (_libelf_allocate_scn(e, (size_t) SHN_UNDEF));
|
||||
return (_libelf_allocate_scn(e, (size_t) SHN_UNDEF));
|
||||
}
|
||||
|
||||
int
|
||||
_libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum)
|
||||
{
|
||||
Elf_Scn *scn;
|
||||
Elf_Scn *scn;
|
||||
|
||||
if (shnum >= SHN_LORESERVE) {
|
||||
if ((scn = _libelf_getscn0(e)) == NULL)
|
||||
return (0);
|
||||
if (shnum >= SHN_LORESERVE) {
|
||||
if ((scn = _libelf_getscn0(e)) == NULL)
|
||||
return (0);
|
||||
|
||||
assert(scn->s_ndx == SHN_UNDEF);
|
||||
assert(scn->s_ndx == SHN_UNDEF);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
scn->s_shdr.s_shdr32.sh_size = shnum;
|
||||
else
|
||||
scn->s_shdr.s_shdr64.sh_size = shnum;
|
||||
if (ec == ELFCLASS32)
|
||||
scn->s_shdr.s_shdr32.sh_size = shnum;
|
||||
else
|
||||
scn->s_shdr.s_shdr64.sh_size = shnum;
|
||||
|
||||
(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
shnum = 0;
|
||||
}
|
||||
shnum = 0;
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
((Elf32_Ehdr *) eh)->e_shnum = shnum;
|
||||
else
|
||||
((Elf64_Ehdr *) eh)->e_shnum = shnum;
|
||||
if (ec == ELFCLASS32)
|
||||
((Elf32_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU;
|
||||
else
|
||||
((Elf64_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU;
|
||||
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int
|
||||
_libelf_setshstrndx(Elf *e, void *eh, int ec, size_t shstrndx)
|
||||
{
|
||||
Elf_Scn *scn;
|
||||
Elf_Scn *scn;
|
||||
|
||||
if (shstrndx >= SHN_LORESERVE) {
|
||||
if ((scn = _libelf_getscn0(e)) == NULL)
|
||||
return (0);
|
||||
if (shstrndx >= SHN_LORESERVE) {
|
||||
if ((scn = _libelf_getscn0(e)) == NULL)
|
||||
return (0);
|
||||
|
||||
assert(scn->s_ndx == SHN_UNDEF);
|
||||
assert(scn->s_ndx == SHN_UNDEF);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
scn->s_shdr.s_shdr32.sh_link = shstrndx;
|
||||
else
|
||||
scn->s_shdr.s_shdr64.sh_link = shstrndx;
|
||||
if (ec == ELFCLASS32)
|
||||
scn->s_shdr.s_shdr32.sh_link = shstrndx;
|
||||
else
|
||||
scn->s_shdr.s_shdr64.sh_link = shstrndx;
|
||||
|
||||
(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
shstrndx = SHN_XINDEX;
|
||||
}
|
||||
shstrndx = SHN_XINDEX;
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx;
|
||||
else
|
||||
((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx;
|
||||
if (ec == ELFCLASS32)
|
||||
((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU;
|
||||
else
|
||||
((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU;
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int
|
||||
_libelf_setphnum(Elf *e, void *eh, int ec, size_t phnum)
|
||||
{
|
||||
Elf_Scn *scn;
|
||||
Elf_Scn *scn;
|
||||
|
||||
if (phnum >= PN_XNUM) {
|
||||
if ((scn = _libelf_getscn0(e)) == NULL)
|
||||
return (0);
|
||||
if (phnum >= PN_XNUM) {
|
||||
if ((scn = _libelf_getscn0(e)) == NULL)
|
||||
return (0);
|
||||
|
||||
assert(scn->s_ndx == SHN_UNDEF);
|
||||
assert(scn->s_ndx == SHN_UNDEF);
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
scn->s_shdr.s_shdr32.sh_info = phnum;
|
||||
else
|
||||
scn->s_shdr.s_shdr64.sh_info = phnum;
|
||||
if (ec == ELFCLASS32)
|
||||
scn->s_shdr.s_shdr32.sh_info = phnum;
|
||||
else
|
||||
scn->s_shdr.s_shdr64.sh_info = phnum;
|
||||
|
||||
(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
phnum = PN_XNUM;
|
||||
}
|
||||
phnum = PN_XNUM;
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
((Elf32_Ehdr *) eh)->e_phnum = phnum;
|
||||
else
|
||||
((Elf64_Ehdr *) eh)->e_phnum = phnum;
|
||||
if (ec == ELFCLASS32)
|
||||
((Elf32_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU;
|
||||
else
|
||||
((Elf64_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU;
|
||||
|
||||
return (1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -22,13 +22,16 @@
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/libelf_fsize.m4,v 1.2 2006/12/18 05:40:01 jkoshy Exp $
|
||||
*/
|
||||
|
||||
#include "libelf.h"
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_fsize.m4 2225 2011-11-26 18:55:54Z jkoshy $");
|
||||
|
||||
/* WARNING: GENERATED FROM __file__. */
|
||||
|
||||
/*
|
||||
* Create an array of file sizes from the elf_type definitions
|
||||
*/
|
||||
@@ -41,15 +44,20 @@ include(SRCDIR`/elf_types.m4')
|
||||
* representations.
|
||||
*/
|
||||
|
||||
/* `Basic' types */
|
||||
/* `Basic' types. */
|
||||
define(`BYTE_SIZE', 1)
|
||||
define(`IDENT_SIZE', `EI_NIDENT')
|
||||
define(`NOTE_SIZE', 1) /* Elf_Note structures have variable length. */
|
||||
|
||||
/* Currently unimplemented types */
|
||||
/* Types that have variable length. */
|
||||
define(`GNUHASH_SIZE', 1)
|
||||
define(`NOTE_SIZE', 1)
|
||||
define(`VDEF_SIZE', 1)
|
||||
define(`VNEED_SIZE', 1)
|
||||
|
||||
/* Currently unimplemented types. */
|
||||
define(`MOVEP_SIZE', 0)
|
||||
|
||||
/* Overrides for 32 bit types that do not exist */
|
||||
/* Overrides for 32 bit types that do not exist. */
|
||||
define(`XWORD_SIZE32', 0)
|
||||
define(`SXWORD_SIZE32', 0)
|
||||
|
||||
@@ -113,7 +121,8 @@ DEFINE_ELF_FSIZES(ELF_TYPE_LIST)
|
||||
DEFINE_ELF_FSIZE(`IDENT',`') # `IDENT' is a pseudo type
|
||||
|
||||
define(`FSIZE',
|
||||
`[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },')
|
||||
`[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
|
||||
')
|
||||
define(`FSIZES',
|
||||
`ifelse($#,1,`',
|
||||
`FSIZE($1)
|
||||
@@ -148,4 +157,3 @@ _libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
|
||||
|
||||
return (sz*c);
|
||||
}
|
||||
|
||||
|
||||
96
ext/libelf/libelf_memory.c
Normal file
96
ext/libelf/libelf_memory.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/*-
|
||||
* Copyright (c) 2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ar.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_memory.c 3013 2014-03-23 06:16:59Z jkoshy $");
|
||||
|
||||
/*
|
||||
* Create an ELF descriptor for a memory image, optionally reporting
|
||||
* parse errors.
|
||||
*/
|
||||
|
||||
Elf *
|
||||
_libelf_memory(unsigned char *image, size_t sz, int reporterror)
|
||||
{
|
||||
Elf *e;
|
||||
int e_class;
|
||||
enum Elf_Error error;
|
||||
unsigned int e_byteorder, e_version;
|
||||
|
||||
assert(image != NULL);
|
||||
assert(sz > 0);
|
||||
|
||||
if ((e = _libelf_allocate_elf()) == NULL)
|
||||
return (NULL);
|
||||
|
||||
e->e_cmd = ELF_C_READ;
|
||||
e->e_rawfile = image;
|
||||
e->e_rawsize = sz;
|
||||
|
||||
#undef LIBELF_IS_ELF
|
||||
#define LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && \
|
||||
(P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 && \
|
||||
(P)[EI_MAG3] == ELFMAG3)
|
||||
|
||||
if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) {
|
||||
e_byteorder = image[EI_DATA];
|
||||
e_class = image[EI_CLASS];
|
||||
e_version = image[EI_VERSION];
|
||||
|
||||
error = ELF_E_NONE;
|
||||
|
||||
if (e_version > EV_CURRENT)
|
||||
error = ELF_E_VERSION;
|
||||
else if ((e_byteorder != ELFDATA2LSB && e_byteorder !=
|
||||
ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class !=
|
||||
ELFCLASS64))
|
||||
error = ELF_E_HEADER;
|
||||
|
||||
if (error != ELF_E_NONE) {
|
||||
if (reporterror) {
|
||||
LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0);
|
||||
(void) _libelf_release_elf(e);
|
||||
return (NULL);
|
||||
}
|
||||
} else {
|
||||
_libelf_init_elf(e, ELF_K_ELF);
|
||||
|
||||
e->e_byteorder = e_byteorder;
|
||||
e->e_class = e_class;
|
||||
e->e_version = e_version;
|
||||
}
|
||||
} else if (sz >= SARMAG &&
|
||||
strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0)
|
||||
return (_libelf_ar_open(e, reporterror));
|
||||
|
||||
return (e);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008-2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,16 +24,14 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <libelf.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "elf32.h"
|
||||
#include "elf64.h"
|
||||
#include "libelf.h"
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_msize.m4 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/* WARNING: GENERATED FROM __file__. */
|
||||
|
||||
struct msize {
|
||||
@@ -44,8 +42,14 @@ struct msize {
|
||||
divert(-1)
|
||||
include(SRCDIR`/elf_types.m4')
|
||||
|
||||
/*
|
||||
* ELF types whose memory representations have a variable size.
|
||||
*/
|
||||
define(BYTE_SIZE, 1)
|
||||
define(GNUHASH_SIZE, 1)
|
||||
define(NOTE_SIZE, 1)
|
||||
define(VDEF_SIZE, 1)
|
||||
define(VNEED_SIZE, 1)
|
||||
|
||||
/*
|
||||
* Unimplemented types.
|
||||
@@ -70,7 +74,8 @@ define(`DEFINE_ELF_MSIZES',
|
||||
DEFINE_ELF_MSIZES(ELF_TYPE_LIST)
|
||||
|
||||
define(`MSIZE',
|
||||
`[ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 },')
|
||||
`[ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 },
|
||||
')
|
||||
define(`MSIZES',
|
||||
`ifelse($#,1,`',
|
||||
`MSIZE($1)
|
||||
|
||||
249
ext/libelf/libelf_open.c
Normal file
249
ext/libelf/libelf_open.c
Normal file
@@ -0,0 +1,249 @@
|
||||
/*-
|
||||
* Copyright (c) 2006,2008-2011 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
#if ELFTC_HAVE_MMAP
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_open.c 3007 2014-03-22 08:10:14Z jkoshy $");
|
||||
|
||||
#define _LIBELF_INITSIZE (64*1024)
|
||||
|
||||
/*
|
||||
* Read from a device file, pipe or socket.
|
||||
*/
|
||||
static void *
|
||||
_libelf_read_special_file(int fd, size_t *fsz)
|
||||
{
|
||||
ssize_t readsz;
|
||||
size_t bufsz, datasz;
|
||||
unsigned char *buf, *t;
|
||||
|
||||
datasz = 0;
|
||||
readsz = 0;
|
||||
bufsz = _LIBELF_INITSIZE;
|
||||
if ((buf = malloc(bufsz)) == NULL)
|
||||
goto resourceerror;
|
||||
|
||||
/*
|
||||
* Read data from the file descriptor till we reach EOF, or
|
||||
* till an error is encountered.
|
||||
*/
|
||||
do {
|
||||
/* Check if we need to expand the data buffer. */
|
||||
if (datasz == bufsz) {
|
||||
bufsz *= 2;
|
||||
if ((t = realloc(buf, bufsz)) == NULL)
|
||||
goto resourceerror;
|
||||
buf = t;
|
||||
}
|
||||
|
||||
do {
|
||||
assert(bufsz - datasz > 0);
|
||||
t = buf + datasz;
|
||||
if ((readsz = read(fd, t, bufsz - datasz)) <= 0)
|
||||
break;
|
||||
datasz += (size_t) readsz;
|
||||
} while (datasz < bufsz);
|
||||
|
||||
} while (readsz > 0);
|
||||
|
||||
if (readsz < 0) {
|
||||
LIBELF_SET_ERROR(IO, errno);
|
||||
goto error;
|
||||
}
|
||||
|
||||
assert(readsz == 0);
|
||||
|
||||
/*
|
||||
* Free up extra buffer space.
|
||||
*/
|
||||
if (bufsz > datasz) {
|
||||
if (datasz > 0) {
|
||||
if ((t = realloc(buf, datasz)) == NULL)
|
||||
goto resourceerror;
|
||||
buf = t;
|
||||
} else { /* Zero bytes read. */
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
free(buf);
|
||||
buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
*fsz = datasz;
|
||||
return (buf);
|
||||
|
||||
resourceerror:
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
error:
|
||||
if (buf != NULL)
|
||||
free(buf);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the contents of the file referenced by the file descriptor
|
||||
* 'fd'.
|
||||
*/
|
||||
|
||||
Elf *
|
||||
_libelf_open_object(int fd, Elf_Cmd c, int reporterror)
|
||||
{
|
||||
Elf *e;
|
||||
void *m;
|
||||
mode_t mode;
|
||||
size_t fsize;
|
||||
struct stat sb;
|
||||
unsigned int flags;
|
||||
|
||||
assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
|
||||
|
||||
if (fstat(fd, &sb) < 0) {
|
||||
LIBELF_SET_ERROR(IO, errno);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
mode = sb.st_mode;
|
||||
fsize = (size_t) sb.st_size;
|
||||
|
||||
/*
|
||||
* Reject unsupported file types.
|
||||
*/
|
||||
if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
|
||||
!S_ISSOCK(mode)) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* For ELF_C_WRITE mode, allocate and return a descriptor.
|
||||
*/
|
||||
if (c == ELF_C_WRITE) {
|
||||
if ((e = _libelf_allocate_elf()) != NULL) {
|
||||
_libelf_init_elf(e, ELF_K_ELF);
|
||||
e->e_byteorder = LIBELF_PRIVATE(byteorder);
|
||||
e->e_fd = fd;
|
||||
e->e_cmd = c;
|
||||
if (!S_ISREG(mode))
|
||||
e->e_flags |= LIBELF_F_SPECIAL_FILE;
|
||||
}
|
||||
|
||||
return (e);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ELF_C_READ and ELF_C_RDWR mode.
|
||||
*/
|
||||
m = NULL;
|
||||
flags = 0;
|
||||
if (S_ISREG(mode)) {
|
||||
|
||||
/*
|
||||
* Reject zero length files.
|
||||
*/
|
||||
if (fsize == 0) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
#if ELFTC_HAVE_MMAP
|
||||
/*
|
||||
* Always map regular files in with 'PROT_READ'
|
||||
* permissions.
|
||||
*
|
||||
* For objects opened in ELF_C_RDWR mode, when
|
||||
* elf_update(3) is called, we remove this mapping,
|
||||
* write file data out using write(2), and map the new
|
||||
* contents back.
|
||||
*/
|
||||
m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
|
||||
|
||||
if (m == MAP_FAILED)
|
||||
m = NULL;
|
||||
else
|
||||
flags = LIBELF_F_RAWFILE_MMAP;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Fallback to a read() if the call to mmap() failed,
|
||||
* or if mmap() is not available.
|
||||
*/
|
||||
if (m == NULL) {
|
||||
if ((m = malloc(fsize)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (read(fd, m, fsize) != (ssize_t) fsize) {
|
||||
LIBELF_SET_ERROR(IO, errno);
|
||||
free(m);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
flags = LIBELF_F_RAWFILE_MALLOC;
|
||||
}
|
||||
} else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL)
|
||||
flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE;
|
||||
else
|
||||
return (NULL);
|
||||
|
||||
if ((e = _libelf_memory(m, fsize, reporterror)) == NULL) {
|
||||
assert((flags & LIBELF_F_RAWFILE_MALLOC) ||
|
||||
(flags & LIBELF_F_RAWFILE_MMAP));
|
||||
if (flags & LIBELF_F_RAWFILE_MALLOC)
|
||||
free(m);
|
||||
#if ELFTC_HAVE_MMAP
|
||||
else
|
||||
(void) munmap(m, fsize);
|
||||
#endif
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* ar(1) archives aren't supported in RDWR mode. */
|
||||
if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
|
||||
(void) elf_end(e);
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
e->e_flags |= flags;
|
||||
e->e_fd = fd;
|
||||
e->e_cmd = c;
|
||||
|
||||
return (e);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -22,133 +22,132 @@
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD: src/lib/libelf/libelf_phdr.c,v 1.2 2006/12/25 02:22:22 jkoshy Exp $
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_phdr.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
void *
|
||||
_libelf_getphdr(Elf *e, int ec)
|
||||
{
|
||||
size_t phnum, phentsize;
|
||||
size_t fsz, msz;
|
||||
uint64_t phoff;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
void *ehdr, *phdr;
|
||||
void (*xlator)(char *_d, char *_s, size_t _c, int _swap);
|
||||
size_t phnum;
|
||||
size_t fsz, msz;
|
||||
uint64_t phoff;
|
||||
Elf32_Ehdr *eh32;
|
||||
Elf64_Ehdr *eh64;
|
||||
void *ehdr, *phdr;
|
||||
int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s,
|
||||
size_t _c, int _swap);
|
||||
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((phdr = (ec == ELFCLASS32 ?
|
||||
(void *) e->e_u.e_elf.e_phdr.e_phdr32 :
|
||||
(void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
|
||||
return (phdr);
|
||||
if ((phdr = (ec == ELFCLASS32 ?
|
||||
(void *) e->e_u.e_elf.e_phdr.e_phdr32 :
|
||||
(void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
|
||||
return (phdr);
|
||||
|
||||
/*
|
||||
* Check the PHDR related fields in the EHDR for sanity.
|
||||
*/
|
||||
/*
|
||||
* Check the PHDR related fields in the EHDR for sanity.
|
||||
*/
|
||||
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (NULL);
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
phnum = e->e_u.e_elf.e_nphdr;
|
||||
phnum = e->e_u.e_elf.e_nphdr;
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
eh32 = (Elf32_Ehdr *) ehdr;
|
||||
phentsize = eh32->e_phentsize;
|
||||
phoff = (uint64_t) eh32->e_phoff;
|
||||
} else {
|
||||
eh64 = (Elf64_Ehdr *) ehdr;
|
||||
phentsize = eh64->e_phentsize;
|
||||
phoff = (uint64_t) eh64->e_phoff;
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
eh32 = (Elf32_Ehdr *) ehdr;
|
||||
phoff = (uint64_t) eh32->e_phoff;
|
||||
} else {
|
||||
eh64 = (Elf64_Ehdr *) ehdr;
|
||||
phoff = (uint64_t) eh64->e_phoff;
|
||||
}
|
||||
|
||||
fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
|
||||
fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
|
||||
|
||||
assert(fsz > 0);
|
||||
assert(fsz > 0);
|
||||
|
||||
if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
|
||||
LIBELF_SET_ERROR(HEADER, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
|
||||
msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(msz > 0);
|
||||
|
||||
if ((phdr = calloc(phnum, msz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((phdr = calloc(phnum, msz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32)
|
||||
e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
|
||||
else
|
||||
e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
|
||||
if (ec == ELFCLASS32)
|
||||
e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
|
||||
else
|
||||
e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
|
||||
|
||||
|
||||
xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
|
||||
(*xlator)(phdr, e->e_rawfile + phoff, phnum,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder));
|
||||
xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
|
||||
(*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
|
||||
e->e_byteorder != LIBELF_PRIVATE(byteorder));
|
||||
|
||||
return (phdr);
|
||||
return (phdr);
|
||||
}
|
||||
|
||||
void *
|
||||
_libelf_newphdr(Elf *e, int ec, size_t count)
|
||||
{
|
||||
void *ehdr, *newphdr, *oldphdr;
|
||||
size_t msz;
|
||||
void *ehdr, *newphdr, *oldphdr;
|
||||
size_t msz;
|
||||
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (e == NULL) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
|
||||
LIBELF_SET_ERROR(SEQUENCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(e->e_class == ec);
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
assert(e->e_version == EV_CURRENT);
|
||||
assert(e->e_class == ec);
|
||||
assert(ec == ELFCLASS32 || ec == ELFCLASS64);
|
||||
assert(e->e_version == EV_CURRENT);
|
||||
|
||||
msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
|
||||
msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(msz > 0);
|
||||
|
||||
newphdr = NULL;
|
||||
if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
newphdr = NULL;
|
||||
if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
|
||||
LIBELF_SET_ERROR(RESOURCE, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASS32) {
|
||||
if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
|
||||
free(oldphdr);
|
||||
e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
|
||||
} else {
|
||||
if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
|
||||
free(oldphdr);
|
||||
e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
|
||||
}
|
||||
if (ec == ELFCLASS32) {
|
||||
if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
|
||||
free(oldphdr);
|
||||
e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
|
||||
} else {
|
||||
if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
|
||||
free(oldphdr);
|
||||
e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
|
||||
}
|
||||
|
||||
e->e_u.e_elf.e_nphdr = count;
|
||||
e->e_u.e_elf.e_nphdr = count;
|
||||
|
||||
elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
|
||||
elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
|
||||
|
||||
return (newphdr);
|
||||
return (newphdr);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -24,28 +24,31 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gelf.h"
|
||||
#include "libelf.h"
|
||||
#include <gelf.h>
|
||||
#include <libelf.h>
|
||||
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_shdr.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
void *
|
||||
_libelf_getshdr(Elf_Scn *s, int ec)
|
||||
{
|
||||
Elf *e;
|
||||
Elf *e;
|
||||
|
||||
if (s == NULL || (e = s->s_elf) == NULL ||
|
||||
e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (s == NULL || (e = s->s_elf) == NULL ||
|
||||
e->e_kind != ELF_K_ELF) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (ec == ELFCLASSNONE)
|
||||
ec = e->e_class;
|
||||
if (ec == ELFCLASSNONE)
|
||||
ec = e->e_class;
|
||||
|
||||
if (ec != e->e_class) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (ec != e->e_class) {
|
||||
LIBELF_SET_ERROR(CLASS, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return ((void *) &s->s_shdr);
|
||||
return ((void *) &s->s_shdr);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2006 Joseph Koshy
|
||||
* Copyright (c) 2006,2008 Joseph Koshy
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -25,10 +25,12 @@
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <libelf.h>
|
||||
|
||||
#include "libelf.h"
|
||||
#include "_libelf.h"
|
||||
|
||||
ELFTC_VCSID("$Id: libelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $");
|
||||
|
||||
/*
|
||||
* Translate to/from the file representation of ELF objects.
|
||||
*
|
||||
@@ -45,97 +47,102 @@ Elf_Data *
|
||||
_libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
|
||||
int elfclass, int direction)
|
||||
{
|
||||
size_t cnt, dsz, fsz, msz;
|
||||
uintptr_t sb, se, db, de;
|
||||
int byteswap;
|
||||
size_t cnt, dsz, fsz, msz;
|
||||
uintptr_t sb, se, db, de;
|
||||
|
||||
if (encoding == ELFDATANONE)
|
||||
encoding = LIBELF_PRIVATE(byteorder);
|
||||
if (encoding == ELFDATANONE)
|
||||
encoding = LIBELF_PRIVATE(byteorder);
|
||||
|
||||
if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
|
||||
dst == NULL || src == NULL || dst == src) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
|
||||
dst == NULL || src == NULL || dst == src) {
|
||||
LIBELF_SET_ERROR(ARGUMENT, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
|
||||
assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
|
||||
assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
|
||||
assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
|
||||
|
||||
if (dst->d_version != src->d_version) {
|
||||
LIBELF_SET_ERROR(UNIMPL, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (dst->d_version != src->d_version) {
|
||||
LIBELF_SET_ERROR(UNIMPL, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (src->d_buf == NULL || dst->d_buf == NULL ||
|
||||
src->d_size == 0) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (src->d_buf == NULL || dst->d_buf == NULL) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)(src->d_type,
|
||||
(size_t) 1, src->d_version)) == 0)
|
||||
return (NULL);
|
||||
if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
|
||||
(src->d_type, (size_t) 1, src->d_version)) == 0)
|
||||
return (NULL);
|
||||
|
||||
msz = _libelf_msize(src->d_type, elfclass, src->d_version);
|
||||
msz = _libelf_msize(src->d_type, elfclass, src->d_version);
|
||||
|
||||
assert(msz > 0);
|
||||
assert(msz > 0);
|
||||
|
||||
if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Determine the number of objects that need to be converted, and
|
||||
* the space required for the converted objects in the destination
|
||||
* buffer.
|
||||
*/
|
||||
if (direction == ELF_TOMEMORY) {
|
||||
cnt = src->d_size / fsz;
|
||||
dsz = cnt * msz;
|
||||
} else {
|
||||
cnt = src->d_size / msz;
|
||||
dsz = cnt * fsz;
|
||||
}
|
||||
/*
|
||||
* Determine the number of objects that need to be converted, and
|
||||
* the space required for the converted objects in the destination
|
||||
* buffer.
|
||||
*/
|
||||
if (direction == ELF_TOMEMORY) {
|
||||
cnt = (size_t) src->d_size / fsz;
|
||||
dsz = cnt * msz;
|
||||
} else {
|
||||
cnt = (size_t) src->d_size / msz;
|
||||
dsz = cnt * fsz;
|
||||
}
|
||||
|
||||
if (dst->d_size < dsz) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if (dst->d_size < dsz) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
sb = (uintptr_t) src->d_buf;
|
||||
se = sb + src->d_size;
|
||||
db = (uintptr_t) dst->d_buf;
|
||||
de = db + dst->d_size;
|
||||
sb = (uintptr_t) src->d_buf;
|
||||
se = sb + (size_t) src->d_size;
|
||||
db = (uintptr_t) dst->d_buf;
|
||||
de = db + (size_t) dst->d_size;
|
||||
|
||||
/*
|
||||
* Check for overlapping buffers. Note that db == sb is
|
||||
* allowed.
|
||||
*/
|
||||
if (db != sb && de > sb && se > db) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
/*
|
||||
* Check for overlapping buffers. Note that db == sb is
|
||||
* allowed.
|
||||
*/
|
||||
if (db != sb && de > sb && se > db) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((direction == ELF_TOMEMORY ? db : sb) %
|
||||
_libelf_malign(src->d_type, elfclass)) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
if ((direction == ELF_TOMEMORY ? db : sb) %
|
||||
_libelf_malign(src->d_type, elfclass)) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
dst->d_type = src->d_type;
|
||||
dst->d_size = dsz;
|
||||
dst->d_type = src->d_type;
|
||||
dst->d_size = dsz;
|
||||
|
||||
if (db == sb && encoding == LIBELF_PRIVATE(byteorder) &&
|
||||
fsz == msz)
|
||||
return (dst); /* nothing more to do */
|
||||
byteswap = encoding != LIBELF_PRIVATE(byteorder);
|
||||
|
||||
(_libelf_get_translator(src->d_type, direction, elfclass))(dst->d_buf,
|
||||
src->d_buf, cnt, encoding != LIBELF_PRIVATE(byteorder));
|
||||
if (src->d_size == 0 ||
|
||||
(db == sb && !byteswap && fsz == msz))
|
||||
return (dst); /* nothing more to do */
|
||||
|
||||
return (dst);
|
||||
if (!(_libelf_get_translator(src->d_type, direction, elfclass))
|
||||
(dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
|
||||
LIBELF_SET_ERROR(DATA, 0);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
55
ext/libelf/native-elf-format
Executable file
55
ext/libelf/native-elf-format
Executable file
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# $Id: native-elf-format 3293 2016-01-07 19:26:27Z emaste $
|
||||
#
|
||||
# Find the native ELF format for a host platform by compiling a
|
||||
# test object and examining the resulting object.
|
||||
#
|
||||
# This script is used if there is no easy way to determine this
|
||||
# information statically at compile time.
|
||||
|
||||
program=`basename $0`
|
||||
tmp_c=`mktemp -u nefXXXXXX`.c
|
||||
tmp_o=`echo ${tmp_c} | sed -e 's/.c$/.o/'`
|
||||
|
||||
trap "rm -f ${tmp_c} ${tmp_o}" 0 1 2 3 15
|
||||
|
||||
touch ${tmp_c}
|
||||
|
||||
echo "/* Generated by ${program} on `date` */"
|
||||
|
||||
cc -c ${tmp_c} -o ${tmp_o}
|
||||
LC_ALL=C readelf -h ${tmp_o} | awk '
|
||||
$1 ~ "Class:" {
|
||||
sub("ELF","",$2); elfclass = $2;
|
||||
}
|
||||
$1 ~ "Data:" {
|
||||
if (match($0, "little")) {
|
||||
elfdata = "LSB";
|
||||
} else {
|
||||
elfdata = "MSB";
|
||||
}
|
||||
}
|
||||
$1 ~ "Machine:" {
|
||||
if (match($0, "Intel.*386")) {
|
||||
elfarch = "EM_386";
|
||||
} else if (match($0, "MIPS")) {
|
||||
elfarch = "EM_MIPS";
|
||||
} else if (match($0, ".*[xX]86-64")) {
|
||||
elfarch = "EM_X86_64";
|
||||
} else if (match($0, "RISC-V")) {
|
||||
elfarch = "EM_RISCV";
|
||||
} else if (match($0, "AArch64")) {
|
||||
elfarch = "EM_AARCH64";
|
||||
} else if (match($0, "ARM")) {
|
||||
elfarch = "EM_ARM";
|
||||
} else {
|
||||
elfarch = "unknown";
|
||||
}
|
||||
}
|
||||
END {
|
||||
printf("#define ELFTC_CLASS ELFCLASS%s\n", elfclass);
|
||||
printf("#define ELFTC_ARCH %s\n", elfarch);
|
||||
printf("#define ELFTC_BYTEORDER ELFDATA2%s\n", elfdata);
|
||||
}'
|
||||
|
||||
Reference in New Issue
Block a user