dnl $NetBSD: libelf_convert.m4,v 1.5 2024/03/03 17:37:34 christos Exp $
/*-
* Copyright (c) 2006-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.
*/
#if HAVE_NBTOOL_CONFIG_H
# include "nbtool_config.h"
#endif
# Generate conversion routines for converting between in-memory and
# file representations of Elf data structures.
#
# These conversions use the type information defined in `elf_types.m4'.
include(SRCDIR`/elf_types.m4')
# For the purposes of generating conversion code, ELF types may be
# classified according to the following characteristics:
#
# 1. Whether the ELF type can be directly mapped to an integral C
# language type. For example, the ELF_T_WORD type maps directly to
# a 'uint32_t', but ELF_T_GNUHASH lacks a matching C type.
#
# 2. Whether the type has word size dependent variants. For example,
# ELT_T_EHDR is represented using C types Elf32_Ehdr and El64_Ehdr,
# and the ELF_T_ADDR and ELF_T_OFF types have integral C types that
# can be 32- or 64- bit wide.
#
# 3. Whether the ELF types has a fixed representation or not. For
# example, the ELF_T_SYM type has a fixed size file representation,
# some types like ELF_T_NOTE and ELF_T_GNUHASH use a variable size
# representation.
#
# We use m4 macros to generate conversion code for ELF types that have
# a fixed size representation. Conversion functions for the remaining
# types are coded by hand.
#
#* Handling File and Memory Representations
#
# `In-memory' representations of an Elf data structure use natural
# alignments and native byte ordering. This allows pointer arithmetic
# and casting to work as expected. On the other hand, the `file'
# representation of an ELF data structure could possibly be packed
# tighter than its `in-memory' representation, and could be of a
# differing byte order. Reading ELF objects that are members of `ar'
# archives present an additional complication: `ar' pads file data to
# even addresses, so file data structures in an archive member
# residing inside an `ar' archive could be at misaligned memory
# addresses when brought into memory.
#
# In summary, casting the `char *' pointers that point to memory
# representations (i.e., source pointers for the *_tof() functions and
# the destination pointers for the *_tom() functions), is safe, as
# these pointers should be correctly aligned for the memory type
# already. However, pointers to file representations have to be
# treated as being potentially unaligned and no casting can be done.
# NOCVT(TYPE) -- Do not generate the cvt[] structure entry for TYPE
define(`NOCVT',`define(`NOCVT_'$1,1)')
# NOFUNC(TYPE) -- Do not generate a conversion function for TYPE
define(`NOFUNC',`define(`NOFUNC_'$1,1)')
# IGNORE(TYPE) -- Completely ignore the type.
define(`IGNORE',`NOCVT($1)NOFUNC($1)')
# Mark ELF types that should not be processed by the M4 macros below.
# Types for which we use functions with non-standard names.
IGNORE(`BYTE') # Uses a wrapper around memcpy().
IGNORE(`NOTE') # Not a fixed size type.
# Types for which we supply hand-coded functions.
NOFUNC(`GNUHASH') # A type with complex internal structure.
NOFUNC(`VDEF') # See MAKE_VERSION_CONVERTERS below.
NOFUNC(`VNEED') # ..
# Unimplemented types.
IGNORE(`MOVEP')
# ELF types that don't exist in a 32-bit world.
NOFUNC(`XWORD32')
NOFUNC(`SXWORD32')
# `Primitive' ELF types are those that are an alias for an integral
# type. As they have no internal structure, they can be copied using
# a `memcpy()', and byteswapped in straightforward way.
#
# Mark all ELF types that directly map to integral C types.
define(`PRIM_ADDR', 1)
define(`PRIM_BYTE', 1)
define(`PRIM_HALF', 1)
define(`PRIM_LWORD', 1)
define(`PRIM_OFF', 1)
define(`PRIM_SWORD', 1)
define(`PRIM_SXWORD', 1)
define(`PRIM_WORD', 1)
define(`PRIM_XWORD', 1)
# Note the primitive types that are size-dependent.
define(`SIZEDEP_ADDR', 1)
define(`SIZEDEP_OFF', 1)
# Generate conversion functions for primitive types.
#
# Macro use: MAKEPRIMFUNCS(ELFTYPE,CTYPE,TYPESIZE,SYMSIZE)
# `$1': Name of the ELF type.
# `$2': C structure name suffix.
# `$3': ELF class specifier for types, one of [`32', `64'].
# `$4': Additional ELF class specifier, one of [`', `32', `64'].
#
# Generates a pair of conversion functions.
define(`MAKEPRIMFUNCS',`
static int
_libelf_cvt_$1$4_tof(unsigned char *dst, size_t dsz, unsigned char *src,
size_t count, int byteswap)
{
Elf$3_$2 t, *s = (Elf$3_$2 *) (uintptr_t) src;
size_t c;
for (c = 0; c < count; c++) {
READ_$1$4(src,t);
SWAP_$1$4(t);
*d++ = t;
}
return (1);
}
')
#
# Handling composite ELF types
#
# SWAP_FIELD(FIELDNAME,ELFTYPE) -- Generate code to swap one field.
define(`SWAP_FIELD',
`ifdef(`SIZEDEP_'$2,
`SWAP_$2'SZ()`(t.$1);
',
`SWAP_$2(t.$1);
')')
# SWAP_MEMBERS(STRUCT) -- Iterate over a structure definition.
define(`SWAP_MEMBERS',
`ifelse($#,1,`/**/',
`SWAP_FIELD($1)SWAP_MEMBERS(shift($@))')')
# SWAP_STRUCT(CTYPE,SIZE) -- Generate code to swap an ELF structure.
define(`SWAP_STRUCT',
`pushdef(`SZ',$2)/* Swap an Elf$2_$1 */
SWAP_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
# WRITE_FIELD(ELFTYPE,FIELDNAME) -- Generate code to write one field.
define(`WRITE_FIELD',
`ifdef(`SIZEDEP_'$2,
`WRITE_$2'SZ()`(dst,t.$1);
',
`WRITE_$2(dst,t.$1);
')')
# WRITE_MEMBERS(ELFTYPELIST) -- Iterate over a structure definition.
define(`WRITE_MEMBERS',
`ifelse($#,1,`/**/',
`WRITE_FIELD($1)WRITE_MEMBERS(shift($@))')')
# WRITE_STRUCT(CTYPE,SIZE) -- Generate code to write out an ELF structure.
define(`WRITE_STRUCT',
`pushdef(`SZ',$2)/* Write an Elf$2_$1 */
WRITE_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
# READ_FIELD(ELFTYPE,CTYPE) -- Generate code to read one field.
define(`READ_FIELD',
`ifdef(`SIZEDEP_'$2,
`READ_$2'SZ()`(s,t.$1);
',
`READ_$2(s,t.$1);
')')
# READ_MEMBERS(ELFTYPELIST) -- Iterate over a structure definition.
define(`READ_MEMBERS',
`ifelse($#,1,`/**/',
`READ_FIELD($1)READ_MEMBERS(shift($@))')')
# READ_STRUCT(CTYPE,SIZE) -- Generate code to read an ELF structure.
define(`READ_STRUCT',
`pushdef(`SZ',$2)/* Read an Elf$2_$1 */
READ_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
# MAKECOMPFUNCS -- Generate converters for composite ELF structures.
#
# When converting data to file representation, the source pointer will
# be naturally aligned for a data structure's in-memory
# representation. When converting data to memory, the destination
# pointer will be similarly aligned.
#
# For in-place conversions, when converting to file representations,
# the source buffer is large enough to hold `file' data. When
# converting from file to memory, we need to be careful to work
# `backwards', to avoid overwriting unconverted data.
#
# Macro use:
# `$1': Name of the ELF type.
# `$2': C structure name suffix.
# `$3': ELF class specifier, one of [`', `32', `64']
define(`MAKECOMPFUNCS', `ifdef(`NOFUNC_'$1$3,`',`
static int
_libelf_cvt_$1$3_tof(unsigned char *dst, size_t dsz, unsigned char *src,
size_t count, int byteswap)
{
Elf$3_$2 t, *s;
size_t c;
(void) &dsz;
s = (Elf$3_$2 *) (uintptr_t) src;
for (c = 0; c < count; c++) {
t = *s++;
if (byteswap) {
SWAP_STRUCT($2,$3)
}
WRITE_STRUCT($2,$3)
}
while (count--) {
s = s0;
READ_STRUCT($2,$3)
if (byteswap) {
SWAP_STRUCT($2,$3)
}
*d-- = t; s0 -= fsz;
}
return (1);
}
')')
# MAKE_TYPE_CONVERTER(ELFTYPE,CTYPE)
#
# Make type convertor functions from the type definition
# of the ELF type:
# - Skip convertors marked as `NOFUNC'.
# - Invoke `MAKEPRIMFUNCS' or `MAKECOMPFUNCS' as appropriate.
define(`MAKE_TYPE_CONVERTER',
`ifdef(`NOFUNC_'$1,`',
`ifdef(`PRIM_'$1,
`ifdef(`SIZEDEP_'$1,
`MAKEPRIMFUNCS($1,$2,32,32)dnl
MAKEPRIMFUNCS($1,$2,64,64)',
`MAKEPRIMFUNCS($1,$2,64)')',
`MAKECOMPFUNCS($1,$2,32)dnl
MAKECOMPFUNCS($1,$2,64)')')')
/*
* C macros to read in various integral values.
*
* Note:
* - The source pointer could be unaligned.
* - Values are read in native byte order.
* - The source pointer is incremented appropriately.
*/
/*
* Sections of type ELF_T_BYTE are never byteswapped, consequently a
* simple memcpy suffices for both directions of conversion.
*/
/*ARGSUSED*/
static int
_libelf_cvt_BYTE_tox(unsigned char *dst, size_t dsz, unsigned char *src,
size_t count, int byteswap)
{
(void) &byteswap;
if (dsz < count)
return (0);
if (dst != src)
(void) memcpy(dst, src, count);
return (1);
}
/*
* Sections of type ELF_T_GNUHASH start with a header containing 4 32-bit
* words. Bloom filter data comes next, followed by hash buckets and the
* hash chain.
*
* Bloom filter words are 64 bit wide on ELFCLASS64 objects and are 32 bit
* wide on ELFCLASS32 objects. The other objects in this section are 32
* bits wide.
*
* Argument `srcsz' denotes the number of bytes to be converted. In the
* 32-bit case we need to translate `srcsz' to a count of 32-bit words.
*/
sz = 4 * sizeof(uint32_t); /* File header is 4 words long. */
if (dsz < sizeof(Elf_GNU_Hash_Header) || srcsz < sz)
return (0);
/* Read in the section header and byteswap if needed. */
READ_WORD(src, nbuckets);
READ_WORD(src, symndx);
READ_WORD(src, maskwords);
READ_WORD(src, shift2);
srcsz -= sz;
if (byteswap) {
SWAP_WORD(nbuckets);
SWAP_WORD(symndx);
SWAP_WORD(maskwords);
SWAP_WORD(shift2);
}
/* Copy the bloom filter and the hash table. */
s64 = (uint64_t *) (uintptr_t) src;
for (n = 0; n < maskwords; n++) {
t64 = *s64++;
if (byteswap)
SWAP_XWORD(t64);
WRITE_WORD64(dst, t64);
}
s32 = (uint32_t *) s64;
for (n = 0; n < nbuckets; n++) {
t32 = *s32++;
if (byteswap)
SWAP_WORD(t32);
WRITE_WORD(dst, t32);
}
srcsz -= sz;
dsz -= sz;
/* Copy out the hash chains. */
if (dsz < srcsz)
return (0);
nchains = (uint32_t) (srcsz / sizeof(uint32_t));
for (n = 0; n < nchains; n++) {
t32 = *s32++;
if (byteswap)
SWAP_WORD(t32);
WRITE_WORD(dst, t32);
}
return (1);
}
/*
* Elf note structures comprise a fixed size header followed by variable
* length strings. The fixed size header needs to be byte swapped, but
* not the strings.
*
* Argument `count' denotes the total number of bytes to be converted.
* The destination buffer needs to be at least `count' bytes in size.
*/
static int
_libelf_cvt_NOTE_tom(unsigned char *dst, size_t dsz, unsigned char *src,
size_t count, int byteswap)
{
uint32_t namesz, descsz, type;
Elf_Note *en;
size_t sz, hdrsz;
if (dsz < count) /* Destination buffer is too small. */
return (0);
hdrsz = 3 * sizeof(uint32_t);
if (count < hdrsz) /* Source too small. */
return (0);
if (!byteswap) {
(void) memcpy(dst, src, count);
return (1);
}
/* Process all notes in the section. */
while (count > hdrsz) {
/* Read the note header. */
READ_WORD(src, namesz);
READ_WORD(src, descsz);
READ_WORD(src, type);
/*
* Return a translator function for the specified ELF section type, conversion
* direction, ELF class and ELF machine.
*/
_libelf_translator_function *
_libelf_get_translator(Elf_Type t, int direction, int elfclass, int elfmachine)
{
assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
assert(t >= ELF_T_FIRST && t <= ELF_T_LAST);