Blame libelf/note_xlate.h

Packit 032894
/* Conversion functions for notes.
Packit 032894
   Copyright (C) 2007, 2009, 2014, 2018 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
static void
Packit 032894
elf_cvt_note (void *dest, const void *src, size_t len, int encode,
Packit 032894
	      bool nhdr8)
Packit 032894
{
Packit 032894
  /* Note that the header is always the same size, but the padding
Packit 032894
     differs for GNU Property notes.  */
Packit 032894
  assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
Packit 032894
Packit 032894
  while (len >= sizeof (Elf32_Nhdr))
Packit 032894
    {
Packit 032894
      /* Convert the header.  */
Packit 032894
      (1 ? Elf32_cvt_Nhdr : Elf64_cvt_Nhdr) (dest, src, sizeof (Elf32_Nhdr),
Packit 032894
					     encode);
Packit 032894
      const Elf32_Nhdr *n = encode ? src : dest;
Packit 032894
Packit 032894
      size_t note_len = sizeof *n;
Packit 032894
Packit 032894
      /* desc needs to be aligned.  */
Packit 032894
      note_len += n->n_namesz;
Packit 032894
      note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
Packit 032894
      if (note_len > len || note_len < sizeof *n)
Packit 032894
	{
Packit 032894
	  /* Header was translated, nothing else.  */
Packit 032894
	  len -= sizeof *n;
Packit 032894
	  src += sizeof *n;
Packit 032894
	  dest += sizeof *n;
Packit 032894
	  break;
Packit 032894
	}
Packit 032894
Packit 032894
      /* data as a whole needs to be aligned.  */
Packit 032894
      note_len += n->n_descsz;
Packit 032894
      note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
Packit 032894
      if (note_len > len || note_len < sizeof *n)
Packit 032894
	{
Packit 032894
	  /* Header was translated, nothing else.  */
Packit 032894
	  len -= sizeof *n;
Packit 032894
	  src += sizeof *n;
Packit 032894
	  dest += sizeof *n;
Packit 032894
	  break;
Packit 032894
	}
Packit 032894
Packit 032894
      /* Copy or skip the note data.  */
Packit 032894
      size_t note_data_len = note_len - sizeof *n;
Packit 032894
      src += sizeof *n;
Packit 032894
      dest += sizeof *n;
Packit 032894
      if (src != dest)
Packit 032894
	memcpy (dest, src, note_data_len);
Packit 032894
Packit 032894
      src += note_data_len;
Packit 032894
      dest += note_data_len;
Packit 032894
      len -= note_len;
Packit 032894
    }
Packit 032894
Packit 032894
    /* Copy over any leftover data unconverted.  Probably part of
Packit 032894
       truncated name/desc data.  */
Packit 032894
    if (unlikely (len > 0) && src != dest)
Packit 032894
      memcpy (dest, src, len);
Packit 032894
}
Packit 032894
Packit 032894
static void
Packit 032894
elf_cvt_note4 (void *dest, const void *src, size_t len, int encode)
Packit 032894
{
Packit 032894
  elf_cvt_note (dest, src, len, encode, false);
Packit 032894
}
Packit 032894
Packit 032894
static void
Packit 032894
elf_cvt_note8 (void *dest, const void *src, size_t len, int encode)
Packit 032894
{
Packit 032894
  elf_cvt_note (dest, src, len, encode, true);
Packit 032894
}