- _libelf_globals::libelf_arch and _libelf_globals::libelf_class are

completely unused.  Remove them.
- _libelf_globals::libelf_byteorder is really all about the byte order of
  the host running libelf, so replace it with a _libelf_host_byteorder()
  internal function that computes the host byte order in a very portable
  way (doesn't rely on BSD-specific header files).
This commit is contained in:
thorpej 2009-12-20 23:23:46 +00:00
parent 4fff15550a
commit a2307e2ec3
8 changed files with 38 additions and 45 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: _libelf.h,v 1.3 2009/12/19 07:47:22 thorpej Exp $ */
/* $NetBSD: _libelf.h,v 1.4 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -44,9 +44,6 @@
#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;
@ -161,6 +158,8 @@ enum {
* Prototypes
*/
unsigned int _libelf_host_byteorder(void);
Elf_Data *_libelf_allocate_data(Elf_Scn *_s);
Elf *_libelf_allocate_elf(void);
Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx);

View File

@ -1,4 +1,4 @@
/* $NetBSD: elf_begin.c,v 1.2 2009/12/19 06:22:25 thorpej Exp $ */
/* $NetBSD: elf_begin.c,v 1.3 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -28,7 +28,7 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/elf_begin.c,v 1.1.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: elf_begin.c,v 1.2 2009/12/19 06:22:25 thorpej Exp $");
__RCSID("$NetBSD: elf_begin.c,v 1.3 2009/12/20 23:23:46 thorpej Exp $");
#include <sys/types.h>
#include <sys/mman.h>
@ -118,7 +118,7 @@ elf_begin(int fd, Elf_Cmd c, Elf *a)
if ((e = _libelf_allocate_elf()) != NULL) {
_libelf_init_elf(e, ELF_K_ELF);
e->e_byteorder = LIBELF_PRIVATE(byteorder);
e->e_byteorder = _libelf_host_byteorder();
e->e_fd = fd;
e->e_cmd = c;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: elf_data.c,v 1.1.1.1 2009/12/19 05:43:39 thorpej Exp $ */
/* $NetBSD: elf_data.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -28,7 +28,7 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/elf_data.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: elf_data.c,v 1.1.1.1 2009/12/19 05:43:39 thorpej Exp $");
__RCSID("$NetBSD: elf_data.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $");
#include <assert.h>
#include <errno.h>
@ -127,7 +127,7 @@ elf_getdata(Elf_Scn *s, Elf_Data *d)
xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
(*xlate)(d->d_buf, e->e_rawfile + sh_offset, count, e->e_byteorder !=
LIBELF_PRIVATE(byteorder));
_libelf_host_byteorder());
return (d);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: elf_scn.c,v 1.3 2009/12/19 07:44:27 thorpej Exp $ */
/* $NetBSD: elf_scn.c,v 1.4 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -28,7 +28,7 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/elf_scn.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: elf_scn.c,v 1.3 2009/12/19 07:44:27 thorpej Exp $");
__RCSID("$NetBSD: elf_scn.c,v 1.4 2009/12/20 23:23:46 thorpej Exp $");
#include <assert.h>
#include <errno.h>
@ -85,7 +85,7 @@ _libelf_load_scn(Elf *e, void *ehdr)
xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
swapbytes = e->e_byteorder != _libelf_host_byteorder();
if (shoff > SSIZE_MAX) {
LIBELF_SET_ERROR(HEADER, 0);
return (0);

View File

@ -1,4 +1,4 @@
/* $NetBSD: libelf.c,v 1.4 2009/12/20 19:38:13 mrg Exp $ */
/* $NetBSD: libelf.c,v 1.5 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -28,38 +28,32 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/libelf.c,v 1.1.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: libelf.c,v 1.4 2009/12/20 19:38:13 mrg Exp $");
__RCSID("$NetBSD: libelf.c,v 1.5 2009/12/20 23:23:46 thorpej Exp $");
#include <sys/param.h>
#if defined(__FreeBSD__)
#include <machine/elf.h>
#endif /* __FreeBSD__ */
#include <machine/endian.h>
#include <libelf.h>
#include "_libelf.h"
#if defined(__NetBSD__)
# if ARCH_ELFSIZE == 64
# define ELF_TARG_DATA ELF64_MACHDEP_ENDIANNESS
# define ELF_TARG_CLASS ELFCLASS64
# else
# define ELF_TARG_DATA ELF32_MACHDEP_ENDIANNESS
# define ELF_TARG_CLASS ELFCLASS32
# endif /* ARCH_ELFSIZE */
#endif /* __NetBSD__ */
struct _libelf_globals _libelf = {
.libelf_arch = 0 /* unused - ELF_ARCH */,
.libelf_byteorder = ELF_TARG_DATA,
.libelf_class = ELF_TARG_CLASS,
.libelf_error = 0,
.libelf_fillchar = 0,
.libelf_version = EV_NONE
};
unsigned int
_libelf_host_byteorder(void)
{
static union {
uint32_t val;
uint8_t bytes[4];
} byte_order_check = {
.val = 0xdeadbeef,
};
return (byte_order_check.bytes[0] == 0xef ? ELFDATA2LSB : ELFDATA2MSB);
}
#if defined(LIBELF_TEST_HOOKS)
int

View File

@ -1,4 +1,4 @@
/* $NetBSD: libelf_ehdr.c,v 1.3 2009/12/19 09:00:56 thorpej Exp $ */
/* $NetBSD: libelf_ehdr.c,v 1.4 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -30,7 +30,7 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/libelf_ehdr.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: libelf_ehdr.c,v 1.3 2009/12/19 09:00:56 thorpej Exp $");
__RCSID("$NetBSD: libelf_ehdr.c,v 1.4 2009/12/20 23:23:46 thorpej Exp $");
#include <assert.h>
#include <gelf.h>
@ -73,7 +73,7 @@ _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
(*xlator)((void *) &scn->s_shdr, e->e_rawfile + (ssize_t)shoff,
(size_t) 1, e->e_byteorder != LIBELF_PRIVATE(byteorder));
(size_t) 1, e->e_byteorder != _libelf_host_byteorder());
#define GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
scn->s_shdr.s_shdr64.M)
@ -182,7 +182,7 @@ _libelf_ehdr(Elf *e, int ec, int allocate)
xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
(*xlator)(ehdr, e->e_rawfile, (size_t) 1,
e->e_byteorder != LIBELF_PRIVATE(byteorder));
e->e_byteorder != _libelf_host_byteorder());
/*
* If extended numbering is being used, read the correct

View File

@ -1,4 +1,4 @@
/* $NetBSD: libelf_phdr.c,v 1.1.1.1 2009/12/19 05:43:41 thorpej Exp $ */
/* $NetBSD: libelf_phdr.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -30,7 +30,7 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/libelf_phdr.c,v 1.2.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: libelf_phdr.c,v 1.1.1.1 2009/12/19 05:43:41 thorpej Exp $");
__RCSID("$NetBSD: libelf_phdr.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $");
#include <assert.h>
#include <gelf.h>
@ -107,7 +107,7 @@ _libelf_getphdr(Elf *e, int ec)
xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
(*xlator)(phdr, e->e_rawfile + phoff, phnum,
e->e_byteorder != LIBELF_PRIVATE(byteorder));
e->e_byteorder != _libelf_host_byteorder());
return (phdr);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: libelf_xlate.c,v 1.1.1.1 2009/12/19 05:43:41 thorpej Exp $ */
/* $NetBSD: libelf_xlate.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $ */
/*-
* Copyright (c) 2006 Joseph Koshy
@ -28,7 +28,7 @@
#include <sys/cdefs.h>
/* __FBSDID("$FreeBSD: src/lib/libelf/libelf_xlate.c,v 1.3.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $"); */
__RCSID("$NetBSD: libelf_xlate.c,v 1.1.1.1 2009/12/19 05:43:41 thorpej Exp $");
__RCSID("$NetBSD: libelf_xlate.c,v 1.2 2009/12/20 23:23:46 thorpej Exp $");
#include <assert.h>
#include <libelf.h>
@ -55,7 +55,7 @@ _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
uintptr_t sb, se, db, de;
if (encoding == ELFDATANONE)
encoding = LIBELF_PRIVATE(byteorder);
encoding = _libelf_host_byteorder();
if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
dst == NULL || src == NULL || dst == src) {
@ -136,11 +136,11 @@ _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
dst->d_size = dsz;
if (src->d_size == 0 ||
(db == sb && encoding == LIBELF_PRIVATE(byteorder) && fsz == msz))
(db == sb && encoding == _libelf_host_byteorder() && fsz == msz))
return (dst); /* nothing more to do */
(_libelf_get_translator(src->d_type, direction, elfclass))(dst->d_buf,
src->d_buf, cnt, encoding != LIBELF_PRIVATE(byteorder));
src->d_buf, cnt, encoding != _libelf_host_byteorder());
return (dst);
}